Median of Two Sorted Arrays

hard

You're given two arrays, nums1 and nums2, each already sorted in non-decreasing order, with lengths m and n. Imagine merging them into one sorted list — return the median of that combined list.

  • If the combined length m + n is odd, the median is the single middle value.
  • If it is even, the median is the average of the two middle values.

Return the answer as a decimal number (for example, 2.50000).

The twist that makes this problem famous: merging takes O(m + n) time, but the required time complexity is O(log (m + n)) — you must find the median without walking through both arrays.

Hints

If the two arrays were already merged, where would the median sit? Do you really need the whole merged list to know what stands in the middle?
The median splits the combined elements into two equal halves, and both arrays are sorted. Think about how many elements each array contributes to the left half.
If you take i elements from the front of nums1, you are forced to take (m + n + 1) / 2 - i from nums2 — and whether that cut is valid can be checked by comparing just four border values. Binary search i over the smaller array.

Common doubts

Even totals average two middle values ((2 + 3) / 2 = 2.5), so the return type is a floating-point number in every case — convert with float(...) / (double) / float64(...) before returning.
The median is simply the median of the other array. The partition solution handles it automatically: the binary search allows i = 0 or i = m, and the -inf / +inf sentinels stand in for the missing borders.
The forced cut j = half - i stays within 0..n only when nums1 is the shorter array; searching the longer one lets j go negative. It also gives the tighter bound O(log min(m, n)).
Yes. The validity test uses <=, so equal values on either side of the cut are perfectly fine — nothing assumes distinctness.

Interview follow-ups

Same partition idea with half replaced by k: binary search how many of the first k elements come from one array, or recursively discard k/2 elements from one array per step. The median is just the special case k = (m + n + 1) / 2.
The two-array cut trick doesn't scale, so switch to binary searching the value domain: guess a value x, count elements <= x in each array with binary search (O(k log n) per guess), and narrow until the count matches the median rank.
The partition solution only ever probes four positions per step, so random-access arrays on disk are fine as-is. For pure streams with no random access you fall back to counting up to the middle — the two-pointer walk — in one pass.

Fun facts

  • The partition trick generalizes beautifully: the median is just the halfway case of finding the k-th smallest element across two sorted arrays, and the identical four-border check solves both.
  • This problem is famous precisely because the binary search runs over cut positions rather than array values — once that reframing clicks, a whole family of partition problems (split arrays, allocate pages, minimize the largest sum) opens up.

Asked at

GoogleAmazonMicrosoftAppleAdobeGoldman Sachs
Frequently Sometimes Occasionally
Example 1
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
The merged order is [1,2,3]; the middle value is 2.
Example 2
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
The merged order is [1,2,3,4]; the median is (2 + 3) / 2 = 2.5.
Constraints

- nums1.length == m - nums2.length == n - 0 <= m <= 1000 - 0 <= n <= 1000 - 1 <= m + n <= 2000 - -10^6 <= nums1[i], nums2[i] <= 10^6 - nums1 and nums2 are each sorted in non-decreasing order

Solve this problem →