|
| 1 | +package binary_search; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * Created by gouthamvidyapradhan on 23/05/2017. |
| 9 | + There are two sorted arrays nums1 and nums2 of size m and n respectively. |
| 10 | +
|
| 11 | + Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). |
| 12 | +
|
| 13 | + Example 1: |
| 14 | + nums1 = [1, 3] |
| 15 | + nums2 = [2] |
| 16 | +
|
| 17 | + The median is 2.0 |
| 18 | + Example 2: |
| 19 | + nums1 = [1, 2] |
| 20 | + nums2 = [3, 4] |
| 21 | +
|
| 22 | + The median is (2 + 3)/2 = 2.5 |
| 23 | +
|
| 24 | + Solution: Works in worst case time complexity of O(log min(m, n)) |
| 25 | +
|
| 26 | + The basic idea is that if you are given two arrays A and B and know |
| 27 | + the length of each, you can check whether an element A[i] is the median in constant |
| 28 | + time. Suppose that the median is A[i]. Since the array is sorted, it is greater than |
| 29 | + exactly i − 1 values in array A. Then if it is the median, it is also greater than exactly |
| 30 | + j = [n / 2] − (i − 1) elements in B. It requires constant time to check if B[j] |
| 31 | + A[i] <= B[j + 1]. If A[i] is not the median, then depending on whether A[i] is greater |
| 32 | + or less than B[j] and B[j + 1], you know that A[i] is either greater than or less than |
| 33 | + the median. Thus you can binary search for A[i] in O(log N) worst-case time |
| 34 | +
|
| 35 | + */ |
| 36 | +public class MedianOfTwoSortedArrays |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Main method |
| 40 | + * @param args |
| 41 | + * @throws Exception |
| 42 | + */ |
| 43 | + public static void main(String[] args) throws Exception |
| 44 | + { |
| 45 | + int[] A = {1,2,5,8,44,45,45}; |
| 46 | + int[] B = {1, 2, 3,4,5,6,7,23,23,23,33,44,45,45,56,77,5555}; |
| 47 | + System.out.println(new MedianOfTwoSortedArrays().findMedianSortedArrays(A, B)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Find median |
| 52 | + * @param nums1 array one |
| 53 | + * @param nums2 array two |
| 54 | + * @return |
| 55 | + */ |
| 56 | + public double findMedianSortedArrays(int[] nums1, int[] nums2) |
| 57 | + { |
| 58 | + if(nums1.length > nums2.length) |
| 59 | + return findMedianSortedArrays(nums2, nums1); //ensure always nums1 is the shortest array |
| 60 | + int T = nums1.length + nums2.length, low = -1, high = -1; |
| 61 | + int median = (T - 1) / 2; |
| 62 | + boolean isOdd = false; |
| 63 | + if((T % 2) != 0) |
| 64 | + isOdd = true; |
| 65 | + |
| 66 | + int s = 0, e = nums1.length - 1; |
| 67 | + while(s <= e) { |
| 68 | + int m = s + (e - s) / 2; |
| 69 | + if((median - m - 1) < 0 || nums1[m] >= nums2[median - m - 1]) { |
| 70 | + e = m - 1; |
| 71 | + low = m; |
| 72 | + high = median - m; |
| 73 | + } |
| 74 | + else s = m + 1; |
| 75 | + } |
| 76 | + |
| 77 | + if(low == -1) { |
| 78 | + if(isOdd) return nums2[median - nums1.length]; |
| 79 | + else return (double)(nums2[median - nums1.length] + nums2[median - nums1.length + 1]) / 2.0D; |
| 80 | + } |
| 81 | + else { |
| 82 | + if(isOdd) return nums1[low] < nums2[high] ? nums1[low] : nums2[high]; |
| 83 | + else { |
| 84 | + //Always sorts maximum of 4 elements hence works in O(1) |
| 85 | + List<Integer> list = new ArrayList<>(); |
| 86 | + list.add(nums1[low]); |
| 87 | + if(low + 1 < nums1.length) |
| 88 | + list.add(nums1[low + 1]); |
| 89 | + list.add(nums2[high]); |
| 90 | + if(high + 1 < nums2.length) |
| 91 | + list.add(nums2[high + 1]); |
| 92 | + Collections.sort(list); |
| 93 | + return (double)(list.get(0) + list.get(1)) / 2.0; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments