Question: There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above Median of two sorted arrays(i.e. array of length 2n). The complexity should be O(log(n))

Question: There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n))

For getting the median of input array { 12, 11, 15, 10, 20 }, first sort the array. We get { 10, 11, 12, 15, 20 } after sorting. Median is the middle element of the sorted array which is 12.

There are different conventions to take median of an array with even number of elements, one can take the mean of the two middle values, or first middle value, or second middle value.

Let us see different methods to get the median of two sorted arrays of size n each. Since size of the set for which we are looking for median is even (2n), we take average of middle two numbers in all below solutions and return floor of the average.

[ad type=”banner”]
Method 1 (Simply count while Merging)
Use merge procedure of merge sort. Keep track of count while comparing elements of two arrays. If count becomes n(For 2n elements), we have reached the median. Take the average of the elements at indexes n-1 and n in the merged array. See the below implementation.
Implementation:

Java Programming

[pastacode lang=”java” manual=”%2F%2F%20A%20Simple%20Merge%20based%20O(n)%20solution%20%0A%2F%2F%20to%20find%20median%20of%20two%20sorted%20arrays%0A%20%0Aclass%20Main%0A%7B%0A%20%20%20%20%2F%2F%20function%20to%20calculate%20median%0A%20%20%20%20static%20int%20getMedian(int%20ar1%5B%5D%2C%20int%20ar2%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%20%20%20%0A%20%20%20%20%20%20%20%20int%20i%20%3D%200%3B%20%20%0A%20%20%20%20%20%20%20%20int%20j%20%3D%200%3B%20%0A%20%20%20%20%20%20%20%20int%20count%3B%0A%20%20%20%20%20%20%20%20int%20m1%20%3D%20-1%2C%20m2%20%3D%20-1%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F*%20Since%20there%20are%202n%20elements%2C%20median%20will%20%0A%20%20%20%20%20%20%20%20%20%20%20be%20average%20of%20elements%20at%20index%20n-1%20and%20%0A%20%20%20%20%20%20%20%20%20%20%20n%20in%20the%20array%20obtained%20after%20merging%20ar1%20%0A%20%20%20%20%20%20%20%20%20%20%20and%20ar2%20*%2F%0A%20%20%20%20%20%20%20%20for%20(count%20%3D%200%3B%20count%20%3C%3D%20n%3B%20count%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Below%20is%20to%20handle%20case%20where%20all%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20elements%20of%20ar1%5B%5D%20are%20smaller%20than%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20smallest(or%20first)%20element%20of%20ar2%5B%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(i%20%3D%3D%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m1%20%3D%20m2%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m2%20%3D%20ar2%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Below%20is%20to%20handle%20case%20where%20all%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elements%20of%20ar2%5B%5D%20are%20smaller%20than%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20smallest(or%20first)%20element%20of%20ar1%5B%5D%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(j%20%3D%3D%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m1%20%3D%20m2%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m2%20%3D%20ar1%5B0%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(ar1%5Bi%5D%20%3C%20ar2%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Store%20the%20prev%20median%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m1%20%3D%20m2%3B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m2%20%3D%20ar1%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Store%20the%20prev%20median%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m1%20%3D%20m2%3B%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m2%20%3D%20ar2%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20(m1%20%2B%20m2)%2F2%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20ar1%5B%5D%20%3D%20%7B1%2C%2012%2C%2015%2C%2026%2C%2038%7D%3B%0A%20%20%20%20%20%20%20%20int%20ar2%5B%5D%20%3D%20%7B2%2C%2013%2C%2017%2C%2030%2C%2045%7D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20int%20n1%20%3D%20ar1.length%3B%0A%20%20%20%20%20%20%20%20int%20n2%20%3D%20ar2.length%3B%0A%20%20%20%20%20%20%20%20if%20(n1%20%3D%3D%20n2)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Median%20is%20%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20getMedian(ar1%2C%20ar2%2C%20n1))%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22arrays%20are%20of%20unequal%20size%22)%3B%0A%20%20%20%20%7D%20%20%20%20%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

Median is 16

Time Complexity: O(n)

Method 2 (By comparing the medians of two arrays)
This method works by first getting medians of the two sorted arrays and then comparing them.

Let ar1 and ar2 be the input arrays.

[ad type=”banner”]

Algorithm:

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

Example:

   ar1[] = {1, 12, 15, 26, 38}
   ar2[] = {2, 13, 17, 30, 45}

For above two arrays m1 = 15 and m2 = 17

For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of the following two subarrays.

   [15, 26, 38] and [2, 13, 17]

Let us repeat the process for above two subarrays:

    m1 = 26 m2 = 13.

m1 is greater than m2. So the subarrays become

  [15, 26] and [13, 17]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
                       = (max(15, 13) + min(26, 17))/2 
                       = (15 + 17)/2
                       = 16
[ad type=”banner”]

Implementation:

[pastacode lang=”c” manual=”%2F%2F%20A%20divide%20and%20conquer%20based%20efficient%20solution%20to%20find%20median%0A%2F%2F%20of%20two%20sorted%20arrays%20of%20same%20size.%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%0Aint%20median(int%20%5B%5D%2C%20int)%3B%20%2F*%20to%20get%20median%20of%20a%20sorted%20array%20*%2F%0A%20%0A%2F*%20This%20function%20returns%20median%20of%20ar1%5B%5D%20and%20ar2%5B%5D.%0A%20%20%20Assumptions%20in%20this%20function%3A%0A%20%20%20Both%20ar1%5B%5D%20and%20ar2%5B%5D%20are%20sorted%20arrays%0A%20%20%20Both%20have%20n%20elements%20*%2F%0Aint%20getMedian(int%20ar1%5B%5D%2C%20int%20ar2%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F*%20return%20-1%20%20for%20invalid%20input%20*%2F%0A%20%20%20%20if%20(n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20if%20(n%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20return%20(ar1%5B0%5D%20%2B%20ar2%5B0%5D)%2F2%3B%0A%20%20%20%20if%20(n%20%3D%3D%202)%0A%20%20%20%20%20%20%20%20return%20(max(ar1%5B0%5D%2C%20ar2%5B0%5D)%20%2B%20min(ar1%5B1%5D%2C%20ar2%5B1%5D))%20%2F%202%3B%0A%20%0A%20%20%20%20int%20m1%20%3D%20median(ar1%2C%20n)%3B%20%2F*%20get%20the%20median%20of%20the%20first%20array%20*%2F%0A%20%20%20%20int%20m2%20%3D%20median(ar2%2C%20n)%3B%20%2F*%20get%20the%20median%20of%20the%20second%20array%20*%2F%0A%20%0A%20%20%20%20%2F*%20If%20medians%20are%20equal%20then%20return%20either%20m1%20or%20m2%20*%2F%0A%20%20%20%20if%20(m1%20%3D%3D%20m2)%0A%20%20%20%20%20%20%20%20return%20m1%3B%0A%20%0A%20%20%20%20%2F*%20if%20m1%20%3C%20m2%20then%20median%20must%20exist%20in%20ar1%5Bm1….%5D%20and%0A%20%20%20%20%20%20%20%20ar2%5B….m2%5D%20*%2F%0A%20%20%20%20if%20(m1%20%3C%20m2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(n%20%25%202%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20getMedian(ar1%20%2B%20n%2F2%20-%201%2C%20ar2%2C%20n%20-%20n%2F2%20%2B1)%3B%0A%20%20%20%20%20%20%20%20return%20getMedian(ar1%20%2B%20n%2F2%2C%20ar2%2C%20n%20-%20n%2F2)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20if%20m1%20%3E%20m2%20then%20median%20must%20exist%20in%20ar1%5B….m1%5D%20and%0A%20%20%20%20%20%20%20%20ar2%5Bm2…%5D%20*%2F%0A%20%20%20%20if%20(n%20%25%202%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%20getMedian(ar2%20%2B%20n%2F2%20-%201%2C%20ar1%2C%20n%20-%20n%2F2%20%2B%201)%3B%0A%20%20%20%20return%20getMedian(ar2%20%2B%20n%2F2%2C%20ar1%2C%20n%20-%20n%2F2)%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20get%20median%20of%20a%20sorted%20array%20*%2F%0Aint%20median(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20if%20(n%252%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20return%20(arr%5Bn%2F2%5D%20%2B%20arr%5Bn%2F2-1%5D)%2F2%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20return%20arr%5Bn%2F2%5D%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20int%20ar1%5B%5D%20%3D%20%7B1%2C%202%2C%203%2C%206%7D%3B%0A%20%20%20%20int%20ar2%5B%5D%20%3D%20%7B4%2C%206%2C%208%2C%2010%7D%3B%0A%20%20%20%20int%20n1%20%3D%20sizeof(ar1)%2Fsizeof(ar1%5B0%5D)%3B%0A%20%20%20%20int%20n2%20%3D%20sizeof(ar2)%2Fsizeof(ar2%5B0%5D)%3B%0A%20%20%20%20if%20(n1%20%3D%3D%20n2)%0A%20%20%20%20%20%20%20%20printf(%22Median%20is%20%25d%22%2C%20getMedian(ar1%2C%20ar2%2C%20n1))%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%22Doesn’t%20work%20for%20arrays%20of%20unequal%20size%22)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output :

Median is 5

Time Complexity: O(logn)

Algorithmic Paradigm: Divide and Conquer

Categorized in: