|
| 1 | +/** |
| 2 | +* Question Link : https://leetcode.com/problems/median-of-two-sorted-arrays/ |
| 3 | +* |
| 4 | +* Problem Statement : Given two sorted arrays nums1 and nums2 of size m and n respectively, |
| 5 | +* return the median of the two sorted arrays. |
| 6 | +* The overall run time complexity should be O(log (m+n)). |
| 7 | +* |
| 8 | +* Intuition : Both arrays are sorted and expected time complexity is logarithmic, we can try using Binary Search |
| 9 | +* |
| 10 | +* Example : |
| 11 | +* nums1 = [1, 3, 4, 7, 10, 12] |
| 12 | +* nums2 = [2, 3, 6, 15] |
| 13 | +* |
| 14 | +* Observation : |
| 15 | +* In Combined array : |
| 16 | +* [1, 2, 3 , 3, 4, 6, 7, 10, 12 ,15] |
| 17 | +* we need two middle elements(in case where m+n is even) or single middle element(in case where m+n is odd) |
| 18 | +* |
| 19 | +* | |
| 20 | +* [1, 2, 3 , 3, 4,| 6, 7, 10, 12 ,15] |
| 21 | +* | |
| 22 | +* |
| 23 | +* If we partition the array in two halves, we need largest element from first half and smallest element from second half |
| 24 | +* |
| 25 | +* From two arrays, partition can be created in following ways |
| 26 | +* |
| 27 | +* |
| 28 | +* nums1 : [1,3,4,7,10] | [12] |
| 29 | +* | |
| 30 | +* nums2 : | [2,3,6,15] |
| 31 | +* | |
| 32 | +* ______________________________________________ |
| 33 | +* |
| 34 | +* nums1 : [1,3,4,7] | [10,12] |
| 35 | +* | |
| 36 | +* nums2 : [2] | [3,6,15] |
| 37 | +* | |
| 38 | +* _______________________________________________ |
| 39 | +* |
| 40 | +* nums1 : [1,3,4] | [7,10,12] |
| 41 | +* | |
| 42 | +* nums2 : [2,3] | [6,15] |
| 43 | +* | |
| 44 | +* _______________________________________________ |
| 45 | +* |
| 46 | +* nums1 : [1,3] | [4,7,10,12] |
| 47 | +* | |
| 48 | +* nums2 : [2,3,6] | [15] |
| 49 | +* | |
| 50 | +* ______________________________________________ |
| 51 | +* |
| 52 | +* nums1 : [1] | [3,4,7,10,12] |
| 53 | +* | |
| 54 | +* nums2 : [2,3,6,15] | |
| 55 | +* | |
| 56 | +* ______________________________________________ |
| 57 | +* |
| 58 | +* If the largest element of nums1[] in left partition |
| 59 | +* is less than or equal to smallest element of nums2[] in right patrtition |
| 60 | +* and |
| 61 | +* If the largest element of nums2[] in left partition |
| 62 | +* is less than or equal to smallest element of nums1[] in right patrtition |
| 63 | +* |
| 64 | +* Then that partion is a valid partition |
| 65 | +* |
| 66 | +*/ |
| 67 | + |
| 68 | +class Solution { |
| 69 | + public double findMedianSortedArrays(int[] nums1, int[] nums2) |
| 70 | + { |
| 71 | + if(nums2.length < nums1.length) |
| 72 | + { |
| 73 | + return findMedianSortedArrays(nums2, nums1); |
| 74 | + //Considering partition length w.r.t. nums1 |
| 75 | + } |
| 76 | + int n = nums1.length; |
| 77 | + int m = nums2.length; |
| 78 | + |
| 79 | + int low = 0; |
| 80 | + int high = n; |
| 81 | + |
| 82 | + while(low<=high) |
| 83 | + { |
| 84 | + int partition1 = (low+high)>>1; |
| 85 | + int partition2 = (n+m+1)/2 - partition1; |
| 86 | + |
| 87 | + //If the partition length becomes 0, we assign it large negative number(negative infinity) |
| 88 | + //left1 is the largest number from nums1 in left partition |
| 89 | + int left1 = (partition1==0)?Integer.MIN_VALUE : nums1[partition1-1]; |
| 90 | + |
| 91 | + //left2 is the largest number from nums2 in left partition |
| 92 | + int left2 = (partition2==0)?Integer.MIN_VALUE : nums2[partition2-1]; |
| 93 | + |
| 94 | + //right1 is the largest number from nums1 in right partition |
| 95 | + int right1 = (partition1==n)?Integer.MAX_VALUE : nums1[partition1]; |
| 96 | + |
| 97 | + //right2 is the largest number from nums2 in right partition |
| 98 | + int right2 = (partition2==m)?Integer.MAX_VALUE : nums2[partition2]; |
| 99 | + |
| 100 | + if(left1<=right2 && left2<=right1) |
| 101 | + { |
| 102 | + //If partition is valid |
| 103 | + if((n+m)%2==0) |
| 104 | + { |
| 105 | + return (max(left1, left2)+min(right1,right2))/2.0; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + return max(left1, left2); |
| 110 | + } |
| 111 | + } |
| 112 | + //else reduce the partition from nums1 |
| 113 | + else if(left1 > right2) |
| 114 | + { |
| 115 | + high = partition1-1; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + low = partition1+1; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return 0.0; |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + //Utility function to return maximum of two numbers |
| 128 | + public int max(int a, int b) |
| 129 | + { |
| 130 | + return a>b? a:b; |
| 131 | + } |
| 132 | + |
| 133 | + //Utility function to return minimun of two numbers |
| 134 | + public int min(int a, int b) |
| 135 | + { |
| 136 | + return a<b? a:b; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments