← Visualizers
Binary Search — Hard LeetCode 4

Median of Two Sorted Arrays / visualized

Binary search the partition point on the smaller array, not a value. Cut both arrays so the combined left half has exactly (m+n+1)/2 elements — once every value on the left is ≤ every value on the right, the median sits right at that cut, no merging required. O(log min(m,n)) instead of O(m+n).

Execution

idle
A (smaller)
B (larger)
Press Run to begin.
0 / 0
Speed

Java · running line

left partition right partition leftMax (l1/l2) rightMin (r1/r2) median value
O(log min(m,n)) time · O(1) space  ·  j = halfLen − i is forced — moving i automatically moves j, so only one array needs a real binary search.