Skip to content
Onsite

Median of Two Sorted Arrays

HardP2Coding

Given two sorted arrays nums1 and nums2, return the median of the combined sorted sequence. When the total count is even, the median is the average of the two middle values.

Your solution should run in O(log(min(m, n))) time. At least one array is non-empty.

Example 1

Input
nums1 = [1,3], nums2 = [2]
Output
2.0

Example 2

Input
nums1 = [1,2], nums2 = [3,4]
Output
2.5

Example 3

Input
nums1 = [], nums2 = [5]
Output
5.0

Constraints

  • 0 <= nums1.length, nums2.length <= 1000, and the total is at least 1
  • -10^6 <= nums1[i], nums2[i] <= 10^6
  • Both arrays are sorted in non-decreasing order

Hints

Run checks the 3 visible tests, Submit all 11.