Skip to content

Commit fffa253

Browse files
committed
Add MedianOfTwoSortedArrays.java new algorithm
1 parent 842ff52 commit fffa253

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
public class MedianOfTwoSortedArrays {
4+
5+
/**
6+
* Finds the median of two sorted arrays in logarithmic time.
7+
*
8+
* @param nums1 the first sorted array
9+
* @param nums2 the second sorted array
10+
* @return the median of the combined sorted array
11+
* @throws IllegalArgumentException if the input arrays are not sorted
12+
*/
13+
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
14+
if (nums1.length > nums2.length) {
15+
return findMedianSortedArrays(nums2, nums1); // Ensure nums1 is the smaller array
16+
}
17+
18+
int m = nums1.length;
19+
int n = nums2.length;
20+
int low = 0;
21+
int high = m;
22+
while (low <= high) {
23+
int partition1 = (low + high) / 2; // Partition in the first array
24+
int partition2 = (m + n + 1) / 2 - partition1; // Partition in the second array
25+
26+
int maxLeft1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1];
27+
int minRight1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1];
28+
int maxLeft2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1];
29+
int minRight2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2];
30+
31+
// Check if partition is valid
32+
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
33+
// If combined array length is odd
34+
if ((m + n) % 2 == 1) {
35+
return Math.max(maxLeft1, maxLeft2);
36+
}
37+
// If combined array length is even
38+
else {
39+
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
40+
}
41+
} else if (maxLeft1 > minRight2) {
42+
high = partition1 - 1;
43+
} else {
44+
low = partition1 + 1;
45+
}
46+
}
47+
48+
throw new IllegalArgumentException("Input arrays are not sorted");
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.divideandconquer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MedianOfTwoSortedArraysTest {
8+
9+
@Test
10+
void testFindMedianSortedArrays() {
11+
// Test case 1: Arrays of equal length
12+
assertEquals(2.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{1, 3}, new int[]{2, 4}));
13+
14+
// Test case 2: Arrays of different lengths
15+
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{1, 3}, new int[]{2}));
16+
17+
// Test case 3: Arrays with even total length
18+
assertEquals(2.5, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{1, 2, 8}, new int[]{3, 4, 5, 6, 7}));
19+
20+
// Test case 4: Arrays with odd total length
21+
assertEquals(3.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{1, 2, 8}, new int[]{3, 4, 5}));
22+
23+
// Test case 5: Single element arrays
24+
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{1}, new int[]{3}));
25+
26+
// Test case 6: Empty arrays
27+
assertEquals(0.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{}, new int[]{0}));
28+
29+
// Test case 7: Same element arrays
30+
assertEquals(2.0, MedianOfTwoSortedArrays.findMedianSortedArrays(new int[]{2, 2, 2}, new int[]{2, 2, 2}));
31+
}
32+
}

0 commit comments

Comments
 (0)