Median of Two Sorted Arrays

Given two sorted arrays, find the median of the combined sorted sequence in logarithmic time. This is the canonical hard selection problem where a binary search over partition positions is optimal.

Input Format

nums1 and nums2 = sorted arrays

Output Format

median of the combined sorted arrays

Constraints

  • 0 <= nums1.length, nums2.length <= 1000; 1 <= nums1.length + nums2.length <= 2000; -10^6 <= nums1[i], nums2[i] <= 10^6

Examples

Example 1:

Input:

nums1 = [1,3]
nums2 = [2]

Output:

2.0

Explanation:

The combined sorted sequence is [1,2,3].

Example 2:

Input:

nums1 = [1,2]
nums2 = [3,4]

Output:

2.5

Explanation:

The combined sorted sequence is [1,2,3,4].

Loading...
Median of Two Sorted Arrays