DSA Tracker

Hard

Median of Two Sorted Arrays

A hard Binary Search problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Search
Sheets
2
Core for
7 roles
Platform
LeetCode

The problem

Given two sorted arrays of integers nums1 and nums2 of sizes m and n, return the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)).

Example 1

Input
nums1 = [1,3], nums2 = [2]
Output
2.0
Why
Merged array is [1,2,3], median is 2.0.

Example 2

Input
nums1 = [1,2], nums2 = [3,4]
Output
2.5
Why
Merged array is [1,2,3,4], 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

How to think about it

Updated 2026-09-09

The median divides a combined collection into two equal halves where all left elements are smaller than all right elements. Once you choose how many items from the smaller array belong in the left half, the count needed from the second array is completely fixed.

Approaches, worst first

  1. Merge arrays

    time O(m + n) · space O(m + n)

    Merge both sorted arrays with two pointers into an auxiliary array of size m + n and take the middle element. Straightforward to write, but creates unnecessary storage and scans every element linearly.

  2. Two-pointer step counting

    time O(m + n) · space O(1)

    Advance through both arrays in sorted order without allocating full storage until reaching the (m + n) / 2 mark. Drops extra memory, but still visits items linearly.

  3. Binary search on shorter array partitionWrite this one

    time O(log(min(m, n))) · space O(1)

    Binary search on the partition cut index of the shorter array. The partition cut of the second array is derived algebraically, and a valid split is confirmed when cross-boundary elements satisfy sorted order.

Where people lose marks · 3
  • Binary searching the longer array allows the derived partition index in the shorter array to fall out of bounds; always enforce nums1 as the shorter array.
  • Partition cuts landing at index 0 or array length require using -infinity and +infinity guards to prevent reading out-of-bounds memory.
  • Integer division when combining the two central elements for an even total length drops the fractional half; cast sums to floating point before dividing by 2.

The theory behind it

Binary Search — the ground this problem stands on. All Binary Search problems

What Binary Search is

Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.

When to reach for it

Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.

How the pattern works

Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.

What each operation costs

OperationTime
find target in sorted arrayO(log n)
find boundary in monotonic rangeO(log n)
What usually goes wrong with Binary Search
  • Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
  • Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
  • Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.

Which roles need this problem

Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Search problems

Problem set and role mapping as of .