|
| 1 | +package org.dojo.leetcode; |
| 2 | + |
| 3 | +import static java.lang.Math.max; |
| 4 | +import static java.lang.Math.min; |
| 5 | + |
| 6 | +public class BinarySearch { |
| 7 | + public double findMedianSortedArrays(int[] nums1, int[] nums2) { |
| 8 | + // S = Smaller array |
| 9 | + // B = Bigger array |
| 10 | + int[] S = nums1.length <= nums2.length ? nums1 : nums2; |
| 11 | + int[] B = nums1.length <= nums2.length ? nums2 : nums1; |
| 12 | + |
| 13 | + // Let M be the merged array = [..S, ..B] |
| 14 | + // Size of a merged array is |
| 15 | + int MSize = S.length + B.length; |
| 16 | + |
| 17 | + // IMPORTANT: Median finding logic depends on whether M has even or odd count of elements |
| 18 | + boolean isEven = MSize % 2 == 0; |
| 19 | + |
| 20 | + // INVARIANT: The median of the merged array divides it into a one and only one correct symmetry: |
| 21 | + // 50% count on the left partition and 50% on the right partition; |
| 22 | + // if isEven, then M == LeftPartitionSize x 2 (due to integer division), |
| 23 | + // else !isEven, then M == LeftPartitionSize x 2 - 1 (M + 1 is even) |
| 24 | + // so for odd sized M, LeftPartitionSize = RightPartionSize + 1, this influences the formula for |
| 25 | + // finding median for odd sized M |
| 26 | + int LEFTPARTITIONSIZE = (MSize + 1) >> 1; // CONSTANT |
| 27 | + |
| 28 | + // Binary search to find the correct element count from S (left partition of S) |
| 29 | + // which goes into the left partition of M |
| 30 | + // Let SLCount = correct number of elements from S (left partition of S) |
| 31 | + // which goes into the left partition of M |
| 32 | + // Then BLCount = correct number of elements from B (left partition of B) |
| 33 | + // which goes into the left partition of M |
| 34 | + // Lemma: LeftPartitionSize (INVARIANT) = SLCount + BLCount; |
| 35 | + // Binary search to determine correct SLCount, over the smaller array to reduce time complexity |
| 36 | + int lowerBoundSLCount = 0, upperBoundSLCount = S.length; |
| 37 | + |
| 38 | + while (lowerBoundSLCount <= upperBoundSLCount) { |
| 39 | + // Test element counts (also partition boundaries) of both S and B |
| 40 | + // which goes into LeftPartition of M |
| 41 | + int testSLCount = (lowerBoundSLCount + upperBoundSLCount) >> 1; |
| 42 | + int testBLCount = LEFTPARTITIONSIZE - testSLCount; |
| 43 | + |
| 44 | + // Values for the above element counts |
| 45 | + int SLMAX = Integer.MIN_VALUE, BLMAX = Integer.MIN_VALUE; |
| 46 | + int SRMIN = Integer.MAX_VALUE, BRMIN = Integer.MAX_VALUE; |
| 47 | + |
| 48 | + // testSLCount is size, hence rightmost value in the Left Partition of S has index testSLCount - 1 |
| 49 | + // Guard for |
| 50 | + if (testSLCount - 1 >= 0) SLMAX = S[testSLCount - 1]; |
| 51 | + // Hence, leftmost value in the Right Partition of S has index testSLCount |
| 52 | + // Guard for |
| 53 | + if (testSLCount < S.length) SRMIN = S[testSLCount]; |
| 54 | + // testBLCount is size, hence rightmost value in the Left Partition of B has index testBLCount - 1 |
| 55 | + // Guard for |
| 56 | + if (testBLCount - 1 >= 0) BLMAX = B[testBLCount - 1]; |
| 57 | + // Hence, leftmost value in the Right Partition of B has index testBLCount |
| 58 | + // Guard for |
| 59 | + if (testBLCount < B.length) BRMIN = B[testBLCount]; |
| 60 | + |
| 61 | + /* |
| 62 | + * Iteration of partitionings to find the correct symmetry: |
| 63 | + * Rotate top (small) and bottom (big) arrays to arrive at the correct partitioning |
| 64 | + * S0, S1, S2 ... S[testSLCount - 1] or SLMAX | SRMIN or S[testSLCount], S[testSLCount + 1], ... S[S.length - 1] |
| 65 | + * B0, B1, B2, ....... B[testBLCount - 1] or BLMAX | BRMIN or B[testBLCount], B[testBLCount + 1], ...... B[B.length - 1] |
| 66 | + */ |
| 67 | + |
| 68 | + if (SLMAX <= BRMIN && BLMAX <= SRMIN) |
| 69 | + return isEven ? calculateMedian(SLMAX, BLMAX, SRMIN, BRMIN) : calculateMedian(SLMAX, BLMAX); |
| 70 | + |
| 71 | + // Counter Clockwise rotation, take higher count of elements from S and |
| 72 | + // take lower count of elements from B to maintain Left Partition Size invariant and |
| 73 | + // to maintain sorted order of M |
| 74 | + else if (BLMAX > SRMIN) lowerBoundSLCount = testSLCount + 1; |
| 75 | + // Clockwise rotation, take lower count of elements from S and |
| 76 | + // take higher count of elements from B to maintain Left Partition Size invariant |
| 77 | + // to maintain sorted order of M |
| 78 | + else if (SLMAX > BRMIN) upperBoundSLCount = testSLCount - 1; |
| 79 | + } |
| 80 | + |
| 81 | + // This case is never executed. |
| 82 | + return 0.0; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Calculate median of combined array M from S and B, when M.length is even. |
| 87 | + * |
| 88 | + * @param SLMAX maximum value from left partition of smaller array |
| 89 | + * @param BLMAX maximum value from left partition of bigger array |
| 90 | + * @param SRMIN minimum value from right partition of smaller array |
| 91 | + * @param BRMIN minimum value from right partition of bigger array |
| 92 | + * @return A double valued median of combined array M from S and B, when M.length is even |
| 93 | + */ |
| 94 | + private static double calculateMedian(int SLMAX, int BLMAX, int SRMIN, int BRMIN) { |
| 95 | + return (max(SLMAX, BLMAX) + min(SRMIN, BRMIN)) / 2.0; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Calculate median of combined array M from S and B, when M.length is odd. |
| 100 | + * |
| 101 | + * @param SLMAX maximum value from left partition of smaller array |
| 102 | + * @param BLMAX maximum value from left partition of bigger array |
| 103 | + * @return A double valued median of combined array M from S and B, when M.length is odd |
| 104 | + */ |
| 105 | + private static double calculateMedian(int SLMAX, int BLMAX) { |
| 106 | + return max(SLMAX, BLMAX); |
| 107 | + } |
| 108 | +} |
0 commit comments