Re: [algogeeks] Re: Array Merge Problem

2011-06-06 Thread Aakash Johari
@ross: I couldn't get reddy's solution. Please explain. On Sun, Jun 5, 2011 at 10:50 PM, Deepak Jha deepak.127.0@gmail.comwrote: the below solution should work given the input array is sorted ( I am assuming ascending order) void rearrangeArray(int[] a, int[] b){ int m = a.length; int

[algogeeks] Re: Array Merge Problem

2011-06-06 Thread ross
@aakash Johari: Let a and b be the 2 arrays. At each stage of the process, if an element of A is greater than B, then swap the largest element of A with the smallest element of B and adjust pointers. A : 2 4 15 12 B : 0.2 1 33 44 Now, 20, therefore swap 0 with 12.. Every step of the process,

Re: [algogeeks] Re: Array Merge Problem

2011-06-05 Thread Deepak Jha
the below solution should work given the input array is sorted ( I am assuming ascending order) void rearrangeArray(int[] a, int[] b){ int m = a.length; int n = b.length; int i = m - 1; int j = 0; while((i =0) (j = n-1)){ if(a[i] b[j]){ int temp = a[i]; a[i] = b[j]; b[j] = temp; } i--; j++; } }

[algogeeks] Re: Array Merge Problem

2011-06-04 Thread rohit
i think solution would be like this eg: A : 1 2 3 B: 0 1.5 4 5 9 Output: A can contain any combination of nos 0,1,1.5 and B should contain 2 3 4 5 9 (in any order.) this example is given by ROSS itself. so sravanreddy solution is right , correct me if i'm wrong. On Jun 3, 8:07 pm, bittu

[algogeeks] Re: Array Merge Problem

2011-06-04 Thread ross
Hi Rohit all, Sorry that there was a small typo in the 'n' 'm' texts. The example given by me is anyway the correct one. Sravan Reddy's solution worked fine. On Jun 4, 10:08 am, rohit rajuljain...@gmail.com wrote: i think solution would be like this eg: A : 1 2 3 B: 0 1.5 4 5 9 Output: A

[algogeeks] Re: Array Merge Problem

2011-06-03 Thread bittu
@sravanreddy...logical bugs if A is size of n B is size m from your example assuming nm so if you want smallest m elements in A then u only capacity of n elements didn't allocate memory so these elements initialized by INT_MIN for m-n nodes so that array A can hold m smallest elements then

Re: [algogeeks] Re: Array Merge Problem

2011-06-03 Thread Aakash Johari
Please try this solution. And tell if it fails at any case. If it works fine, I will tell the logic. #include stdio.h #include stdlib.h void merge(int *a, int m, int *b, int n) { int i, j, k; int t; i = j = 0; k = -1; while ( i m a[i] b[j] ) { i++;

Re: [algogeeks] Re: Array Merge Problem

2011-06-03 Thread Ravinder Kumar
it can be done in O(m) . Use something like binary search . code is here ... #includestdio.h void splitMN(int a[],int m , int b[], int n){ int al = 0 , bl = 0 ; int ah = m-1 , bh = n-1 ; int ai = (ah+al+1)/2; int bi = (bh+bl+1)/2; while(ai+bi!=m){ printf(Enter ai

[algogeeks] Re: Array Merge Problem

2011-05-28 Thread sravanreddy001
Maintain a pointer A_end = m-1; doing a comparision something similar to merge sort int i=0;j=0; while (i m){ if (a[i] b[j]) i++; else{ swap(a[A_end],b[j]) A_end --; j++; } } This runs in O(m) time and no extra space, also the sort order is not guarenteed. -- You received this

[algogeeks] Re: Array Merge Problem

2011-05-28 Thread ross
@sravanreddy: Hey, Nice Solution :) cool! On May 29, 7:44 am, sravanreddy001 sravanreddy...@gmail.com wrote: Maintain a pointer A_end = m-1; doing a comparision something similar to merge sort int i=0;j=0; while (i m){  if (a[i] b[j])   i++;  else{   swap(a[A_end],b[j])   A_end --;  

Re: [algogeeks] Re: Array Merge Problem

2011-05-28 Thread ankit sambyal
@sravanreddy: it won't work. Try 3,91,9 and 90,1,8,2,5 . correct me if i m wrong. Thanks, Ankit Sambyal On Sat, May 28, 2011 at 9:16 PM, ross jagadish1...@gmail.com wrote: @sravanreddy: Hey, Nice Solution :) cool! On May 29, 7:44 am, sravanreddy001 sravanreddy...@gmail.com wrote:

Re: [algogeeks] Re: Array Merge Problem

2011-05-28 Thread immanuel kingston
@Ankit, The input should be 2 sorted arrays Thanks, Immanuel On Sun, May 29, 2011 at 10:48 AM, ankit sambyal ankitsamb...@gmail.comwrote: @sravanreddy: it won't work. Try 3,91,9 and 90,1,8,2,5 . correct me if i m wrong. Thanks, Ankit Sambyal On Sat, May 28, 2011 at 9:16 PM, ross

Re: [algogeeks] Re: Array Merge Problem

2011-05-28 Thread ankit sambyal
Oh I didn't read the question properly, Thanks for pointing out... On Sat, May 28, 2011 at 10:28 PM, immanuel kingston kingston.imman...@gmail.com wrote: @Ankit, The input should be 2 sorted arrays Thanks, Immanuel On Sun, May 29, 2011 at 10:48 AM, ankit sambyal