15. Median of Two Sorted Arrays

Hard · Array

Given two sorted arrays `nums1` and `nums2` of sizes `m` and `n` respectively, return the **median** of the two sorted arrays combined. The overall run time complexity should be O(log(m+n)).

The median is the middle value when all elements are sorted. If the total number of elements is even, the median is the average of the two middle values. Return the result as a floating-point number.

You may assume that both arrays are sorted in non-decreasing order and that at least one of the arrays is non-empty.

Examples

Example 1
Input: nums1 = [1, 3], nums2 = [2]
Output: 2.0
Explanation: Merged array is [1, 2, 3]. The median is 2.0 (middle element of 3 elements).
Example 2
Input: nums1 = [1, 2], nums2 = [3, 4]
Output: 2.5
Explanation: Merged array is [1, 2, 3, 4]. The median is (2 + 3) / 2 = 2.5.

Constraints