|
| 1 | +package com.bl.median_of_two_sorted_arrays; |
| 2 | + |
| 3 | +/** |
| 4 | + * This is the solution to the LeetCode problem: 4. Median of Two Sorted Arrays |
| 5 | + * |
| 6 | + * @author Børre A. Opedal Lunde |
| 7 | + * @since 2024.01.25 |
| 8 | + */ |
| 9 | +@SuppressWarnings("ManualMinMaxCalculation") |
| 10 | +public class Solution { |
| 11 | + |
| 12 | + // This solution is made with readability in mind. It is not the most |
| 13 | + // efficient solution, but it should be understandable. And that's what |
| 14 | + // matters more for me. |
| 15 | + // |
| 16 | + // There are many ways to optimise for performance and memory usage. For |
| 17 | + // example by using bitwise operations for division, inlining methods and |
| 18 | + // constants, using the original arrays instead of copying them, and |
| 19 | + // reducing the number of helper variables and methods, etc. |
| 20 | + |
| 21 | + private static final int HYPOTHETICAL_NEGATIVE_INFINITY = 0x80000000; // Integer minimum value. |
| 22 | + private static final int HYPOTHETICAL_POSITIVE_INFINITY = 0x7fffffff; // Integer maximum value. |
| 23 | + |
| 24 | + public double findMedianSortedArrays(int[] nums1, int[] nums2) { |
| 25 | + |
| 26 | + // Determine the smallest and largest array. |
| 27 | + final int[] leftArray = getSmallestArrayOf(nums1, nums2); |
| 28 | + final int[] rightArray = getLargestArrayOf(nums1, nums2); |
| 29 | + |
| 30 | + // Calculate the sizes of the arrays and their combined length. This |
| 31 | + // will be used in the partitioning of the arrays. |
| 32 | + final int leftArraySize = leftArray.length; |
| 33 | + final int rightArraySize = rightArray.length; |
| 34 | + final int combinedArraysLength = leftArraySize + rightArraySize; |
| 35 | + |
| 36 | + // According to the problem description, the arrays should never be |
| 37 | + // empty. If they are, then we throw an exception. |
| 38 | + if (leftArraySize == 0 && rightArraySize == 0) { |
| 39 | + throw new IllegalArgumentException("The arrays are empty."); |
| 40 | + } |
| 41 | + |
| 42 | + // Low and high for the binary search. |
| 43 | + int low = 0; |
| 44 | + int high = leftArraySize; |
| 45 | + |
| 46 | + // Binary search. |
| 47 | + while (low <= high) { |
| 48 | + |
| 49 | + // In simple steps, this is what goes on: |
| 50 | + // |
| 51 | + // 1. Calculate the partition indices for the arrays. |
| 52 | + // |
| 53 | + // 2. Get the left and right values of the partitions. |
| 54 | + // |
| 55 | + // 3. Check if the left side of the partition is less than or equal |
| 56 | + // to the right side of the partition. |
| 57 | + // |
| 58 | + // 4. If the left side is less than or equal to the right side, |
| 59 | + // then we have found the median. |
| 60 | + // |
| 61 | + // 5. If the left side is greater than the right side, then we need |
| 62 | + // to move the partition to the left. |
| 63 | + // |
| 64 | + // 6. If the left side is less than the right side, then we need to |
| 65 | + // move the partition to the right. |
| 66 | + |
| 67 | + // Calculate the partition indices for the arrays. |
| 68 | + final int leftArrayPartitionIndex = (low + high) / 2; |
| 69 | + final int rightArrayPartitionIndex = (combinedArraysLength + 1) / 2 - leftArrayPartitionIndex; |
| 70 | + |
| 71 | + // Get values at partition indices. |
| 72 | + final int leftArrayLeftValue = getLeftValueOfPartition(leftArray, leftArrayPartitionIndex); |
| 73 | + final int leftArrayRightValue = getRightValueOfPartition(leftArray, leftArrayPartitionIndex); |
| 74 | + |
| 75 | + final int rightArrayLeftValue = getLeftValueOfPartition(rightArray, rightArrayPartitionIndex); |
| 76 | + final int rightArrayRightValue = getRightValueOfPartition(rightArray, rightArrayPartitionIndex); |
| 77 | + |
| 78 | + // The left side is OK if the left value is less than or equal to |
| 79 | + // the right value. Equally, the right side is OK if the right |
| 80 | + // value is less than or equal to the left value. |
| 81 | + final boolean leftSideIsOk = leftArrayLeftValue <= rightArrayRightValue; |
| 82 | + final boolean rightSideIsOk = rightArrayLeftValue <= leftArrayRightValue; |
| 83 | + |
| 84 | + // If both sides are OK, then we can calculate the median. |
| 85 | + if (leftSideIsOk && rightSideIsOk) { |
| 86 | + |
| 87 | + // Get the maximum of the left values and the minimum of the |
| 88 | + // right values. The maximum and minimum are the closest values |
| 89 | + // to the middle. |
| 90 | + final int leftValue = getMaximumOf(leftArrayLeftValue, rightArrayLeftValue); |
| 91 | + final int rightValue = getMinimumOf(leftArrayRightValue, rightArrayRightValue); |
| 92 | + |
| 93 | + final double median; |
| 94 | + if (isEven(combinedArraysLength)) { |
| 95 | + // When the combined length is even, we need to calculate |
| 96 | + // the average of the two middle values. |
| 97 | + median = (leftValue + rightValue) / 2.0; |
| 98 | + } else { |
| 99 | + // It is easier when the combined length is odd. Then the |
| 100 | + // median is the middle (left) value. |
| 101 | + median = leftValue; |
| 102 | + } |
| 103 | + |
| 104 | + System.out.printf("Returning median: %f%n", median); |
| 105 | + return median; |
| 106 | + } |
| 107 | + |
| 108 | + // Move the partition to the left if the left side is not OK. |
| 109 | + else if (! leftSideIsOk) { |
| 110 | + high = leftArrayPartitionIndex - 1; |
| 111 | + } |
| 112 | + |
| 113 | + // Vice versa for the right side. |
| 114 | + else { |
| 115 | + low = leftArrayPartitionIndex + 1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Given the constraints of the problem, we should never reach this |
| 120 | + // point. If we reach here regardless, then something is wrong. |
| 121 | + throw new UnsupportedOperationException("Something went wrong."); |
| 122 | + } |
| 123 | + |
| 124 | + private static int getLeftValueOfPartition(final int[] array, final int index) { |
| 125 | + if (index <= 0) { |
| 126 | + // There is no left value at this index, so we return the smallest |
| 127 | + // possible value. We can then be sure that the value to its right |
| 128 | + // is greater or equal to it. |
| 129 | + return HYPOTHETICAL_NEGATIVE_INFINITY; |
| 130 | + } |
| 131 | + return array[index - 1]; |
| 132 | + } |
| 133 | + |
| 134 | + private static int getRightValueOfPartition(final int[] array, final int index) { |
| 135 | + if (index >= array.length) { |
| 136 | + // The same goes for the right side, just the other way around. |
| 137 | + return HYPOTHETICAL_POSITIVE_INFINITY; |
| 138 | + } |
| 139 | + return array[index]; |
| 140 | + } |
| 141 | + |
| 142 | + private static int[] getSmallestArrayOf(int[] a, int[] b) { |
| 143 | + return a.length <= b.length ? a : b; |
| 144 | + } |
| 145 | + |
| 146 | + private static int[] getLargestArrayOf(int[] a, int[] b) { |
| 147 | + return a.length <= b.length ? b : a; |
| 148 | + } |
| 149 | + |
| 150 | + private static boolean isEven(final int number) { |
| 151 | + return number % 2 == 0; |
| 152 | + } |
| 153 | + |
| 154 | + private static int getMaximumOf(final int a, final int b) { |
| 155 | + return a >= b ? a : b; |
| 156 | + } |
| 157 | + |
| 158 | + private static int getMinimumOf(final int a, final int b) { |
| 159 | + return a >= b ? b : a; |
| 160 | + } |
| 161 | +} |
0 commit comments