You are given two sorted arrays of integers nums1 and nums2.
nums1
has length
m
,
nums2
has length
n
.
Your task is to find the median of the combined multiset formed by all elements of nums1 and nums2.
m + n
in a way that makes the complexity worse than (O(m + n)).
Definition of median:
m + n
is odd, the median is the middle element after the two arrays' elements are conceptually merged and sorted.
m + n
is even, the median is the average (as a floating point number) of the two middle elements after the two arrays' elements are conceptually merged and sorted.
Function signature (conceptual):
nums1
,
nums2
.
Examples:
nums1 = [1, 3]
,
nums2 = [2]
→ merged is
[1, 2, 3]
, median =
2.0
.
nums1 = [1, 2]
,
nums2 = [3, 4]
→ merged is
[1, 2, 3, 4]
, median =
(2 + 3) / 2 = 2.5
.
Explain the algorithm you would use to achieve (O(\log(m + n))) time and its correctness, then implement it in code.