Skip to content

Median of Two Sorted Arrays

HardBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

Given two sorted lists nums1 and nums2 (either may be empty, but not both), return the median of all their values combined. The median is the middle value of the merged order, or the average of the two middle values when the total count is even. Your solution must run in O(log(m + n)) time.

nums1 = [1, 3], nums2 = [2]      →  2.0
nums1 = [1, 2], nums2 = [3, 4]   →  2.5
nums1 = [],     nums2 = [1]      →  1.0

Constraints

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