You are given two sorted arrays of integers nums1 and nums2 in non-decreasing order.
Let the lengths be m = nums1.length and n = nums2.length. Either array may be empty, but not both at the same time.
If you were to conceptually merge them into a single sorted array merged of length m + n, the median is defined as:
-
If
m + n
is odd: the element at index
(m + n) / 2
(0-based) in
merged
.
-
If
m + n
is even: the average of the two middle elements at indices
(m + n) / 2 - 1
and
(m + n) / 2
in
merged
.
Task
Implement a function that returns the median of the two sorted arrays without actually merging them.
-
Function signature (conceptual):
-
Input:
nums1: integer[]
,
nums2: integer[]
-
Output:
median: float
(or double)
Constraints (you may assume)
-
0 ≤ m, n ≤ 10^5
-
m + n ≥ 1
-
Each array is individually sorted in non-decreasing order.
-
Integer values fit in a 32-bit signed integer range.
-
Target time complexity:
O(log(m + n))
.
-
Extra space complexity:
O(1)
beyond input storage.