Median of Two Sorted Arrays

Hard
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Median of Two Sorted Arrays

Approach

Ensure nums1 is the shorter array. Binary search on the partition index i of nums1, compute j for nums2 so the left half has exactly half the total elements. Valid partition means max(left sides) <= min(right sides).

Complexity Analysis

Time
O(log(min(m,n)))
Space
O(1)

Pattern

Binary Search (Boundary Finding)

Why It Works

Binary search on where to partition the smaller array. The combined partition must have exactly half the total elements, and max of left side must be less than or equal to min of right side.

Updated Feb 2026