450 views

1 Answer

0 0 votes

Using divide and conquer , we get the complexity of above as O(logn).We so this as :

1) Calculate the medians m1 and m2 of the input arrays ar1[]
   and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
     return m1 (or m2)
3) 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])
4) 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_|])
5) Repeat the above process until size of both the subarrays
   becomes 2.
6) 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

For reference(implementation details) , have a look at :

http://www.geeksforgeeks.org/median-of-two-sorted-arrays/

Position:
Show:

Related questions

0 0 votes
1 answers 1 answer
1.3k
1.3k views
iarnav asked Jan 11, 2018
1,328 views
How to find log of the first n natural numbers?$T(n)= \log 1+ \log 2+ \log 3+\ldots + \log n$ // How to proceed from here?
2 2 votes
1 answers 1 answer
1.2k
1.2k views
richa07 asked Oct 11, 2016
1,212 views