Missing Number
Missing Number is LeetCode 268 (Easy). The array nums holds n distinct numbers, all taken from the range 0 to n.
- That range has
n + 1numbers, so exactly one of them is absent. - Return the absent number.
The follow-up asks for O(n) time and O(1) extra space, which rules out sorting and rules out a set of seen values.
- n == nums.length
- 1 <= n <= 10⁴
- 0 <= nums[i] <= n
- All the numbers of nums are unique.
Intuition
If nothing were missing, the numbers would be 0, 1, 2, …, n, and their sum is known without looking at the array: n(n + 1) / 2. The array has every one of those numbers except one, so its sum falls short by exactly the missing value.
So to find the missing number, start from the full total and subtract each number in the array. Each subtraction removes a number that is present. When the array runs out, the only number never subtracted is the missing one, and it is all that is left of the total.
Distinct values from a known, gap-free range with one value absent: compare what the range should add up to with what the array adds up to. Find All Numbers Disappeared in an Array handles several gaps, and Single Number uses the same XOR cancelling trick on pairs.
Approach
Before reading on, write down what 0 + 1 + … + n equals for n = 3, then compare it with the sum of [3, 0, 1]. Aim for one pass and two variables.
Two ways to solve it
Subtract every value from 0 + 1 + … + n.
- Easy to explain: plain arithmetic.
- Safe here: the total stays far below the
intlimit.
XOR every index, every value and n; matching pairs cancel.
- No overflow at any size: XOR never carries.
- Harder to see why: it relies on
a ^ a = 0.
Both read the array once and keep one number. The sum version is the easier one to explain, so the steps, code and live demo use it; the XOR code follows the demo.
Work out the full total
Let n be the array length. The complete range 0 to n sums to n * (n + 1) / 2. That needs no loop, because the formula pairs the smallest and largest numbers, which always add to n.
Take each present number out
Walk the array once and subtract every value from the total. Values are distinct, so each number in the range is removed at most once.
Return what is left
After the loop, the total holds the sum of the numbers that never appeared. Exactly one number is absent, so that sum is the answer, including when the answer is 0 or n.
Code in Python, C++ and Java
XOR of indices and values
Start with n, then XOR in every index i and every value nums[i]. Together the indices and n list each number from 0 to n once. Each value in the array matches one of those and cancels it, because a ^ a = 0. The one number with no partner survives.
Common pitfalls
Using 1 to n instead of 0 to n
expected = n * (n - 1) // 2
expected = n * (n + 1) // 2
The array has n values but the range has n + 1, because it starts at 0 and ends at n. With the shorter formula, [0, 1] gives 1 − 1 = 0 instead of 2, and every answer comes out n too small.
Checking neighbours after sorting
nums.sort()
for i in range(len(nums)):
if nums[i] != i:
return inums.sort()
for i in range(len(nums)):
if nums[i] != i:
return i
return len(nums)When the missing number is n itself, every position matches its value and the loop never returns. [0, 1] falls through and returns nothing. Sorting also costs O(n log n), which misses the follow-up.
Complexity
int holds it.Missing Number FAQ
Can the sum overflow on bigger inputs?
In languages with fixed-size integers, yes: for n above about 46,000, n * (n + 1) passes the 32-bit limit. Subtracting as you go (total += i + 1 - nums[i]) keeps the running value small, and the XOR version below never grows past the largest number at all.
What if more than one number is missing?
One equation can only pin down one unknown. With two gaps, use both the sum and the sum of squares, or mark seen values in place by flipping signs, as in Find All Numbers Disappeared in an Array.