Missing Number
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.
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.
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?
An integer array nums of length n with distinct values in 0..n.
The single value in 0..n that does not appear in nums.
n = 3, so the values should be 0, 1, 2, 3. Only 2 is not there.
n = 2, so the values should be 0, 1, 2. 0 and 1 are present, so the missing one is 2 = n.
n = 9. Every value from 0 to 9 appears except 8.
- • n == nums.length
- • 1 ≤ n ≤ 10^4
- • 0 ≤ nums[i] ≤ n
- • All values in nums are distinct.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
How many values could appear, and how many slots does the array have?
- 2.
If I had a checklist of 0..n, how would I tick it off in one pass?
- 3.
Is there a natural index where the value 0 could live? The value 1? The value v?
- 4.
Which value has no index it could live at?
- 5.
Once every value sits at its own index, how do I spot the hole?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each k in 0..n, scan the array to see if k is there | O(N²) | O(1) |
| Better | Mark each value in a boolean array of size n + 1, then find the unmarked one | O(N) | O(N) |
| Optimal | Swap each value v < n into index v, park n, then find the first slot without its own value | O(N) | O(1) |
Why does this belong to this pattern?
- “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.
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.
Don't make this mistake
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.
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.
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.
On [0, 1] every slot is correct after placement. The missing value is the one with no slot at all: n = 2.
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.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Cyclic Sort translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.