dsa · hard

Median of Two Sorted Arrays

nums1 and nums2 are each sorted non-decreasing. Return the median of the values from both arrays together (the middle of the combined multiset). If the combined length is odd, return the single middle value. If it is even, return the average of the two central values. The answer is a floating number.

Arguments

Either array may be empty, but not both.

Example

nums1 = [1,3], nums2 = [2] together in order are 1, 2, 3. One middle → 2.0.

nums1 = [1,2], nums2 = [3,4] together are 1, 2, 3, 4. Two middles 2 and 3, average → 2.5.

Constraints

0 <= nums1.length, nums2.length <= 1000 nums1.length + nums2.length >= 1 nums1 and nums2 are each sorted in non-decreasing order -10^6 <= nums1[i], nums2[i] <= 10^6 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,3]
[2]

Expected:
2.0

Example 2

Input:
[1,2]
[3,4]

Expected:
2.5

Open in the Dojo editor