is making a third array, to merge both sorted array in one larger sorted array and then finding the median right approach
or is there any other approach?
Right approach for challenge question
please explain the binary search approach
- Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively. - If m1 and m2 both are equal then we are done.
return m1 (or m2) - If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0…|n/2|])
b) From m2 to last element of ar2 (ar2[|n/2|…n-1]) - If m2 is greater than m1, then median is present in one
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|n/2|…n-1])
b) From first element of ar2 to m2 (ar2[0…|n/2|]) - Repeat the above process until size of both the subarrays
becomes 2. - If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
which approach is time and space friendly?
obviously the binary search
can u provide the code for binary approach
whats wrong with my merging code???
nothing …
mergin logic will also work fine. have said the same in my earlier response as well
whats wrong with it???
put both the while loop out of the main while loop