• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 04, 2022 |1.2K Views
Median of two sorted arrays of same size
Description
Discussion

here 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)). 


Since the size of the set for which we are looking for the median is even (2n), we need to take the average of the middle two numbers and return the floor of the average.


Use the 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.


Median of two sorted arrays of same size : https://www.geeksforgeeks.org/median-of-two-sorted-arrays/