2Brute2Best
🔥0

Missing Number

Easy·LeetCode #268Basic Anchor
LeetCode
30-Second Mental ModelCyclic Sort — missing number (home of v is index v)
Complexity:O(N²)→O(N)
Key Insight

The checklist from the better version can live inside nums. Give value v the home nums[v]. Swap every value into its home; the value n has no home, so leave it parked. After that, index i holds i exactly when i is present, and the parked n (if any) sits on the one hole.

Core Validity Invariant

Keep swapping nums[i] into its home while nums[i] < n and nums[nums[i]] != nums[i]. After that, the answer is the first i with nums[i] != i, or n if every slot is right.

01

Problem

You are given an array nums of n distinct numbers. Every number is taken from the range 0, 1, 2, …, n.

That range has n + 1 values but the array only has n of them, so exactly one value is left out. Return that value.

Follow-up: can you do it in O(N) time and O(1) extra space?

Input

An integer array nums of length n with distinct values in 0..n.

Output

The single value in 0..n that does not appear in nums.

Example 1
Input: nums = [3, 0, 1]
Output: 2

n = 3, so the values should be 0, 1, 2, 3. Only 2 is not there.

Example 2
Input: nums = [0, 1]
Output: 2

n = 2, so the values should be 0, 1, 2. 0 and 1 are present, so the missing one is 2 = n.

Example 3
Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output: 8

n = 9. Every value from 0 to 9 appears except 8.

Constraints
  • • n == nums.length
  • • 1 ≤ n ≤ 10^4
  • • 0 ≤ nums[i] ≤ n
  • • All values in nums are distinct.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    How many values could appear, and how many slots does the array have?

  2. 2.

    If I had a checklist of 0..n, how would I tick it off in one pass?

  3. 3.

    Is there a natural index where the value 0 could live? The value 1? The value v?

  4. 4.

    Which value has no index it could live at?

  5. 5.

    Once every value sits at its own index, how do I spot the hole?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceFor each k in 0..n, scan the array to see if k is thereO(N²)O(1)
BetterMark each value in a boolean array of size n + 1, then find the unmarked oneO(N)O(N)
OptimalSwap each value v < n into index v, park n, then find the first slot without its own valueO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “numbers in the range 0..n”Every value fits an index — value v can live at index v.
  • “n numbers, one missing”After putting everyone home, the one empty home is the answer.
  • “O(1) extra space”No seen[] array — the input array must be the checklist.
Values in 0..n+find what is missing+O(1) extra space→Cyclic Sort — missing number (home of v is index v)

This is the anchor for the "missing" sub-pattern: put each value at its home, then the slot that does not hold its own value tells you what is missing. Two details are special here. The values start at 0, so the home of v is index v (not v − 1). And there is one value too many for the slots — n has no home, so the swap loop must skip it. Find All Numbers Disappeared uses values 1..n (home v − 1) and adds duplicates; First Missing Positive adds junk values that must be skipped.

10

Don't make this mistake

Trying to place n at index n
✗ while (nums[i] != nums[nums[i]]) // nums[i] can be n swap(nums[i], nums[nums[i]]);
✓ while (nums[i] < n && nums[i] != nums[nums[i]]) swap(nums[i], nums[nums[i]]);

This is the pattern-confusion trap for this problem. The values go up to n, but the indices only go up to n − 1. When nums[i] == n, nums[nums[i]] reads past the end of the array. The value n simply has no home — skip it and leave it parked.

Using the 1..n home rule (v − 1)
✗ while (nums[i] >= 1 && nums[nums[i] - 1] != nums[i]) swap(nums[i], nums[nums[i] - 1]); ... if (nums[i] != i + 1) return i + 1;
✓ while (nums[i] < n && nums[nums[i]] != nums[i]) swap(nums[i], nums[nums[i]]); ... if (nums[i] != i) return i;

Also pattern confusion: copying the template from First Missing Positive or Find All Numbers Disappeared, where values are 1..n and the home is v − 1. Here the values start at 0, so the home is v itself. With the wrong rule the value 0 has no home and is skipped, and the scan checks for the wrong thing: on [1, 2] every slot looks correct, so the code never reports 0, which is the real answer.

Using if instead of while
✗ if (nums[i] < n && nums[i] != nums[nums[i]]) swap(nums[i], nums[nums[i]]);
✓ while (nums[i] < n && nums[i] != nums[nums[i]]) swap(nums[i], nums[nums[i]]);

After one swap, the value that lands at i may also need to move. On [1, 2, 3, 0] the single swaps give [2, 1, 0, 3]: the 2 left behind at index 0 is never looked at again. The scan then says 0 is missing, but the answer is 4.

Forgetting the "n is missing" case
✗ for (int i = 0; i < n; i++) if (nums[i] != i) return i; return -1;
✓ for (int i = 0; i < n; i++) if (nums[i] != i) return i; return n;

On [0, 1] every slot is correct after placement. The missing value is the one with no slot at all: n = 2.

Using the sum trick on a different problem
✗ return n * (n + 1) / 2 - accumulate(nums.begin(), nums.end(), 0);

It is a nice O(1)-space answer for this problem, and fine to use in an interview here. But it only works when exactly one value is missing and none repeat. As soon as there are duplicates or several missing values (the next questions in this sub-pattern), the sum cannot tell you which ones. Learn the cyclic sort version — it carries over.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Cyclic Sort translate into production architectures and low-level memory mechanics.