Problem Statement

Median of Two Sorted Arrays

You get two lists of numbers, nums1 and nums2. Each list is already sorted from smallest to largest. Your job is to find the median of all the numbers combined. The median is the middle value: if you lined every number up in order, the median is the one in the center. If there is an even count of numbers, the median is the average of the two middle ones. The catch is speed. You need to do this in O(log (m+n)) time, where m and n are the sizes of the two lists. The "log" part means the work should shrink fast as the lists grow, which rules out the simple idea of merging both lists and walking to the middle.

hardBinary SearchBinary SearchTime: O(log(min(m,n))) · Space: O(1)

Signals to notice

find median of two sorted arraysO(log(m+n)) requiredbinary search on partition

Brute force first

Merge both arrays and find the middle element. Correct but doesn't meet the requirement. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Binary search on the partition point of the smaller array. Partition both arrays such that left halves contain the smaller half of all elements. The median is at the partition boundary. O(log(min(m,n))). Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Trace it on nums1=[1,3], nums2=[2]

Setup: len(nums1)=2 > len(nums2)=1 -> swap; nums1=[2], nums2=[1,3]; m=1, n=2; left=0, right=1
Iter1: i=(0+1)//2=0, j=(1+2+1)//2-0=2; maxL1=-inf, minR1=2, maxL2=nums2[1]=3, minR2=+inf
Iter1 check: maxL1<=minR2 (-inf<=inf) ok, but maxL2<=minR1 (3<=2) FAILS; not maxL1>minR2 -> left=i+1=1
Iter2: i=(1+1)//2=1, j=2-1=1; maxL1=nums1[0]=2, minR1=+inf, maxL2=nums2[0]=1, minR2=nums2[1]=3
Iter2 check: 2<=3 ok AND 1<=inf ok -> partition balanced
(m+n)=3 is odd -> return float(max(maxL1,maxL2))=max(2,1)=2.0

What must stay true

If you partition both arrays such that every element in the left halves ≤ every element in the right halves, and left halves have exactly ⌊(m+n+1)/2⌋ elements total, then the median is at the boundary. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Shape of the loop

ensure A is the shorter array; m, n = len(A), len(B); lo, hi = 0, m
while lo <= hi:
    i = (lo+hi)//2;  j = (m+n+1)//2 - i        # j is forced by i
    L1,R1 = A[i-1] or -inf, A[i] or +inf;  L2,R2 = B[j-1] or -inf, B[j] or +inf
    if L1<=R2 and L2<=R1: return median from max(L1,L2)/min(R1,R2) by parity
    elif L1 > R2: hi = i-1   else: lo = i+1     # slide partition in A

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Binary searching both arrays — only search the smaller one. The other array's partition is determined: partitionB = (m+n+1)/2 - partitionA. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Binary Search Pattern