Skip to content

Add MedianOfTwoSortedArrays.java new algorithm #5554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
* divideandconquer
* [BinaryExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/BinaryExponentiation.java)
* [ClosestPair](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/ClosestPair.java)
* [MedianOfTwoSortedArrays](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java)
* [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/SkylineAlgorithm.java)
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
* dynamicprogramming
Expand Down Expand Up @@ -773,6 +774,7 @@
* divideandconquer
* [BinaryExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/BinaryExponentiationTest.java)
* [ClosestPairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/ClosestPairTest.java)
* [MedianOfTwoSortedArraysTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArraysTest.java)
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
* dynamicprogramming
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.thealgorithms.divideandconquer;

public final class MedianOfTwoSortedArrays {

private MedianOfTwoSortedArrays() {
}

/**
* Finds the median of two sorted arrays in logarithmic time.
*
* @param nums1 the first sorted array
* @param nums2 the second sorted array
* @return the median of the combined sorted array
* @throws IllegalArgumentException if the input arrays are not sorted
*/
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
return findMedianSortedArrays(nums2, nums1); // Ensure nums1 is the smaller array
}

int m = nums1.length;
int n = nums2.length;
int low = 0;
int high = m;
while (low <= high) {
int partition1 = (low + high) / 2; // Partition in the first array
int partition2 = (m + n + 1) / 2 - partition1; // Partition in the second array

int maxLeft1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1];
int minRight1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1];
int maxLeft2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1];
int minRight2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2];

// Check if partition is valid
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
// If combined array length is odd
if (((m + n) & 1) == 1) {
return Math.max(maxLeft1, maxLeft2);
}
// If combined array length is even
else {
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
}
} else if (maxLeft1 > minRight2) {
high = partition1 - 1;
} else {
low = partition1 + 1;
}
}

throw new IllegalArgumentException("Input arrays are not sorted");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.divideandconquer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class MedianOfTwoSortedArraysTest {

@ParameterizedTest
@MethodSource("provideTestCases")
void testFindMedianSortedArrays(int[] nums1, int[] nums2, double expectedMedian) {
assertEquals(expectedMedian, MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2));
}

private static Stream<Arguments> provideTestCases() {
return Stream.of(
// Test case 1: Arrays of equal length
Arguments.of(new int[] {1, 3}, new int[] {2, 4}, 2.5),

// Test case 2: Arrays of different lengths
Arguments.of(new int[] {1, 3}, new int[] {2}, 2.0),

// Test case 3: Arrays with even total length
Arguments.of(new int[] {1, 2, 8}, new int[] {3, 4, 5, 6, 7}, 4.5),

// Test case 4: Arrays with odd total length
Arguments.of(new int[] {1, 2, 8}, new int[] {3, 4, 5}, 3.5),

// Test case 5: Single element arrays
Arguments.of(new int[] {1}, new int[] {3}, 2.0),

// Test case 6: Empty arrays
Arguments.of(new int[] {}, new int[] {0}, 0.0),

// Test case 7: Same element arrays
Arguments.of(new int[] {2, 2, 2}, new int[] {2, 2, 2}, 2.0));
}
}