The “Find Median of Two Sorted Arrays” problem involves finding the median element of the merged array formed by combining two sorted arrays. The median is the middle element in a sorted array, or the average of the two middle elements if the array has an even length.
Step-by-Step Approach:
- Calculate the total length of the merged array,
totalLength, which is the sum of the lengths of both input arrays. - Determine the indices of the two middle elements of the merged array,
mid1andmid2. IftotalLengthis odd, bothmid1andmid2will be the same, representing the median element. IftotalLengthis even,mid1andmid2will be the middle elements used to calculate the median. - Merge the two sorted arrays while keeping track of the elements up to the indices
mid1andmid2in variableselement1andelement2, respectively. This can be done with a two-pointer approach. - If
totalLengthis odd, the median is the maximum ofelement1andelement2. - If
totalLengthis even, the median is the average ofelement1andelement2.
Java Solution:
public class FindMedianSortedArrays {
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int totalLength = nums1.length + nums2.length;
int mid1 = (totalLength - 1) / 2;
int mid2 = totalLength / 2;
int pointer1 = 0, pointer2 = 0;
int element1 = 0, element2 = 0;
for (int i = 0; i <= mid2; i++) {
element1 = element2;
if (pointer1 < nums1.length && (pointer2 >= nums2.length || nums1[pointer1] < nums2[pointer2])) {
element2 = nums1[pointer1++];
} else {
element2 = nums2[pointer2++];
}
}
return (totalLength % 2 == 0) ? (element1 + element2) / 2.0 : element2;
}
public static void main(String[] args) {
int[] nums1 = {1, 3};
int[] nums2 = {2};
System.out.println("Median of the merged array: " + findMedianSortedArrays(nums1, nums2));
}
}
Python Solution:
def find_median_sorted_arrays(nums1, nums2):
total_length = len(nums1) + len(nums2)
mid1, mid2 = (total_length - 1) // 2, total_length // 2
pointer1, pointer2 = 0, 0
element1, element2 = 0, 0
for i in range(mid2 + 1):
element1 = element2
if pointer1 < len(nums1) and (pointer2 >= len(nums2) or nums1[pointer1] < nums2[pointer2]):
element2 = nums1[pointer1]
pointer1 += 1
else:
element2 = nums2[pointer2]
pointer2 += 1
return (element1 + element2) / 2 if total_length % 2 == 0 else element2
if __name__ == "__main__":
nums1 = [1, 3]
nums2 = [2]
print("Median of the merged array:", find_median_sorted_arrays(nums1, nums2))
Explanation of Java/Python Solution:
The Java and Python solutions both implement the “Find Median of Two Sorted Arrays” problem using a two-pointer approach to merge the sorted arrays and find the median.
The findMedianSortedArrays function in Java and find_median_sorted_arrays function in Python accept two sorted arrays nums1 and nums2 as input and return the median of the merged array.
The example arrays nums1 = [1, 3] and nums2 = [2] demonstrate the calculation of the median for the merged array [1, 2, 3]. The expected output for this example is 2.0, which is the median of the merged array. If there is an odd number of elements in the merged array, the median will be a single element. If there is an even number of elements, the median will be the average of the two middle elements.