0 0 votes Given two sorted arrays of n elements with distinct integers.How much time it will take to find middle of union if these two arrays.? Can be done in logn but how? Algorithms algorithms + – rahul sharma 5 738 views answer comment Share Follow Print See 1 comment 1 1 comment reply santhoshdevulapally commented Dec 13, 2016 i moved by santhoshdevulapally Jan 6, 2017 reply Follow flag TIME COMPLEXITY OF THE ALGORITHM IS O(logn) 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: arr[1]={2,13,18,22,24} arr[2]={6,12,23,26,32} m1=18 and m2=23. m1<m2 so arr[1]={n/2......n-1} and arr[2]={0.....n/2} then arr[1]={18,22,24} &arr[2]={6,12,23} repeat this algorithm median=20. http://www.geeksforgeeks.org/median-of-two-sorted-arrays/ 0 0 replyShare Please log in or register to add a comment.