What is Median of Sorted Arrays in O(log n) complexity ?
What is Median of Sorted Arrays in O(log n) complexity? [closed]



Best Answer

Other Answer1


comments:

Other Answer2
Median of two sorted arraysQuestion: 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))Median: In probability theory and statistics, a median is described as the number separating the higher half of a sample, a population, or a probability distribution, from the lower half. The median of a finite list of numbers can be found by arranging all the numbers from lowest value to highest value and picking the middle one. 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 are taking average of two middle two numbers in all below solutions. 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 n1 and n in the merged array. See the below implementation. Implementation:
Time Complexity: O(n) Space Complexity: O(1) 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. 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...n1]) 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...n1]) b) From first element of ar2 to m2 (ar2[0...n/2]) 4) Repeat the above process until size of both the subarrays becomes 2. 5) If size of the two arrays is 2 then use below formula to get the median. Median = (max(ar1[0], ar2[0]) + min(ar11, ar21))/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(ar11, ar21))/2 = (max(15, 13) + min(26, 17))/2 = (15 + 17)/2 = 16 Implementation:
Time Complexity: O(logn) Space Complexity: O(1) Algorithmic Paradigm: Divide and Conquer Method 3 (By doing binary search for the median): The basic idea is that if you are given two arrays ar1[] and ar2[] and know the length of each, you can check whether an element ar1[i] is the median in constant time. Suppose that the median is ar1[i]. Since the array is sorted, it is greater than exactly i1 values in array ar1[]. Then if it is the median, it is also greater than exactly j = n – i – 1 elements in ar2[]. It requires constant time to check if ar2[j] For two arrays ar1 and ar2, first do binary search in ar1[]. If you reach at the end (left or right) of the first array and don’t find median, start searching in the second array ar2[]. 1) Get the middle element of ar1[] using array indexes left and right. Let index of the middle element be i. 2) Calculate the corresponding index j of ar2[] j = n – i – 1 3) If ar1[i] >= ar2[j] and ar1[i] Example: ar1[] = {1, 5, 7, 10, 13} ar2[] = {11, 15, 23, 30, 45} Middle element of ar1[] is 7. Let us compare 7 with 23 and 30, since 7 smaller than both 23 and 30, move to right in ar1[]. Do binary search in {10, 13}, this step will pick 10. Now compare 10 with 15 and 23. Since 10 is smaller than both 15 and 23, again move to right. Only 13 is there in right side now. Since 13 is greater than 11 and smaller than 15, terminate here. We have got the median as 12 (average of 11 and 13) Implementation:
Time Complexity: O(logn) Space Complexity: O(1) Algorithmic Paradigm: Divide and Conquer 

comments:

Other Answer3
Do anything you want for
Edit: this is not fully complete:

Relate
 Median of 5 sorted arrays
 Why partition sorted arrays for finding median?
 Median of BST in O(logn) time complexity
 Sorted array and finding sum with complexity O(n)
 find median of two sorted arrays  I get developark exception
 What is the complexity of inserting into sorted link list in bigO notation?
 Merging two sorted arrays into a third one can be done in O(n)?
 Find the median of the sum of the arrays
 Median Algorithm in O(log n)
 merging sorted arrays [duplicate]
 merge sorted arrays
 MongoDB: sorted arrays
 Algorithm to merge sorted arrays
 Algorithm : Median of two sorted array of different sizes
 Big O notation, Complexity