Skip to content

Commit 0295b4d

Browse files
authored
Enhance README with solution steps and code example
Added detailed steps and implementation for solving the Median of Two Sorted Arrays problem in Java.
1 parent 94a3525 commit 0295b4d

File tree

1 file changed

+69
-1
lines changed
  • src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays

1 file changed

+69
-1
lines changed

src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,72 @@ The overall run time complexity should be `O(log (m+n))`.
2929
* `0 <= m <= 1000`
3030
* `0 <= n <= 1000`
3131
* `1 <= m + n <= 2000`
32-
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
32+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
33+
34+
To solve the Median of Two Sorted Arrays problem in Java using a `Solution` class, we'll follow these steps:
35+
36+
1. Define a `Solution` class with a method named `findMedianSortedArrays`.
37+
2. Calculate the total length of the combined array (m + n).
38+
3. Determine the middle index or indices of the combined array based on its length (for both even and odd lengths).
39+
4. Implement a binary search algorithm to find the correct position for partitioning the two arrays such that elements to the left are less than or equal to elements on the right.
40+
5. Calculate the median based on the partitioned arrays.
41+
6. Handle edge cases where one or both arrays are empty or where the combined length is odd or even.
42+
7. Return the calculated median.
43+
44+
Here's the implementation:
45+
46+
```java
47+
public class Solution {
48+
49+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
50+
int m = nums1.length;
51+
int n = nums2.length;
52+
int totalLength = m + n;
53+
int left = (totalLength + 1) / 2;
54+
int right = (totalLength + 2) / 2;
55+
return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
56+
}
57+
58+
private int findKth(int[] nums1, int i, int[] nums2, int j, int k) {
59+
if (i >= nums1.length) return nums2[j + k - 1];
60+
if (j >= nums2.length) return nums1[i + k - 1];
61+
if (k == 1) return Math.min(nums1[i], nums2[j]);
62+
63+
int midVal1 = (i + k / 2 - 1 < nums1.length) ? nums1[i + k / 2 - 1] : Integer.MAX_VALUE;
64+
int midVal2 = (j + k / 2 - 1 < nums2.length) ? nums2[j + k / 2 - 1] : Integer.MAX_VALUE;
65+
66+
if (midVal1 < midVal2) {
67+
return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
68+
} else {
69+
return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
70+
}
71+
}
72+
73+
public static void main(String[] args) {
74+
Solution solution = new Solution();
75+
76+
// Test cases
77+
int[] nums1_1 = {1, 3};
78+
int[] nums2_1 = {2};
79+
System.out.println("Example 1 Output: " + solution.findMedianSortedArrays(nums1_1, nums2_1));
80+
81+
int[] nums1_2 = {1, 2};
82+
int[] nums2_2 = {3, 4};
83+
System.out.println("Example 2 Output: " + solution.findMedianSortedArrays(nums1_2, nums2_2));
84+
85+
int[] nums1_3 = {0, 0};
86+
int[] nums2_3 = {0, 0};
87+
System.out.println("Example 3 Output: " + solution.findMedianSortedArrays(nums1_3, nums2_3));
88+
89+
int[] nums1_4 = {};
90+
int[] nums2_4 = {1};
91+
System.out.println("Example 4 Output: " + solution.findMedianSortedArrays(nums1_4, nums2_4));
92+
93+
int[] nums1_5 = {2};
94+
int[] nums2_5 = {};
95+
System.out.println("Example 5 Output: " + solution.findMedianSortedArrays(nums1_5, nums2_5));
96+
}
97+
}
98+
```
99+
100+
This implementation provides a solution to the Median of Two Sorted Arrays problem in Java with a runtime complexity of O(log(min(m, n))).

0 commit comments

Comments
 (0)