xxxxxxxxxx
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
const length1 = nums1.length
const length2 = nums2.length
const totalLength = length1 + length2
let index1 = 0
let index2 = 0
let medianValue = [0, 0]
for (let i = 0; i <= Math.floor(totalLength / 2); i++) {
if (nums1[index1] < nums2[index2] || index2 == length2) {
medianValue.shift()
medianValue.push(nums1[index1])
index1++
} else {
medianValue.shift()
medianValue.push(nums2[index2])
index2++
}
}
if (totalLength % 2 === 0) {
medianValue = (medianValue[0] + medianValue[1]) / 2
} else {
medianValue = medianValue[1]
}
return medianValue
};
xxxxxxxxxx
public class MedianOfTwoSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
// Check if num1 is smaller than num2
// If not, then we will swap num1 with num2
if (nums1.length > nums2.length) {
return findMedianSortedArrays(nums2, nums1);
}
// Lengths of two arrays
int m = nums1.length;
int n = nums2.length;
// Pointers for binary search
int start = 0;
int end = m;
// Binary search starts from here
while (start <= end) {
// Partitions of both the array
int partitionNums1 = (start + end) / 2;
int partitionNums2 = (m + n + 1) / 2 - partitionNums1;
// Edge cases
// If there are no elements left on the left side after partition
int maxLeftNums1 = partitionNums1 == 0 ? Integer.MIN_VALUE : nums1[partitionNums1 - 1];
// If there are no elements left on the right side after partition
int minRightNums1 = partitionNums1 == m ? Integer.MAX_VALUE : nums1[partitionNums1];
// Similarly for nums2
int maxLeftNums2 = partitionNums2 == 0 ? Integer.MIN_VALUE : nums2[partitionNums2 - 1];
int minRightNums2 = partitionNums2 == n ? Integer.MAX_VALUE : nums2[partitionNums2];
// Check if we have found the match
if (maxLeftNums1 <= minRightNums2 && maxLeftNums2 <= minRightNums1) {
// Check if the combined array is of even/odd length
if ((m + n) % 2 == 0) {
return (Math.max(maxLeftNums1, maxLeftNums2) + Math.min(minRightNums1, minRightNums2)) / 2.0;
} else {
return Math.max(maxLeftNums1, maxLeftNums2);
}
}
// If we are too far on the right, we need to go to left side
else if (maxLeftNums1 > minRightNums2) {
end = partitionNums1 - 1;
}
// If we are too far on the left, we need to go to right side
else {
start = partitionNums1 + 1;
}
}
// If we reach here, it means the arrays are not sorted
throw new IllegalArgumentException();
}
}