Missing Number
Pattern: Cyclic sort
Values 0 to n, one missing, in n slots. Send each value v to index v (n has no slot and stays put). The first index that does not hold its own number is the missing one.
The problem
You are given an array of n different numbers taken from 0 to n. Exactly one number in that range is missing.
Return it. For example, [3, 0, 1] gives 2, [0, 1] gives 2, and [9, 6, 4, 2, 3, 5, 7, 0, 1] gives 8.
The function name missingNumber matches LeetCode. Use the link above for the exact statement and limits. The cyclic
sort below reorders the array. If you must not change it, the sum or XOR methods under Brute force are better.
Think first
- If nothing were missing, which index would each value live at?
- With
nslots and values up ton, which value has no slot? - After every value is in its home, how do you spot the missing one?
Hints
Brute force, and why it fails
Check each number from 0 to n with a linear search: O(n²). A hash set is O(n) time and memory. Other O(1) memory
methods: subtract the array's sum from n(n + 1)/2, or XOR every index and value together.
The key insight
When values and indexes share a range, each value already knows where it belongs. One round of swapping values home leaves the missing number's slot as the only wrong one.
Dry run
nums = [3, 0, 1], so n = 3 and the values come from 0 to 3. Value v belongs at index v. At index 0 sits 3, which has no slot, so move on.
The plan
i = 0
while i < n:
v = nums[i]
if v < n and nums[v] != v: swap nums[i], nums[v]
else: i = i + 1
for j from 0 to n - 1:
if nums[j] != j: return j
return n
Complexity
- Time
O(n)- Space
O(1)
Each swap puts one value home for good, so there are at most n swaps, plus n moves of i.
Edge cases
- 0 is missing, as in
[1]: 0. nis missing, as in[0, 1]: every index matches, so returnn.- One element
[0]: 1.
I've built my logic, show the code
Java
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int i = 0;
while (i < n) {
int v = nums[i];
if (v < n && nums[v] != v) { // v belongs at index v; send it there
nums[i] = nums[v];
nums[v] = v;
} else {
i++; // v is home already, or is n, which has no slot
}
}
for (int j = 0; j < n; j++) {
if (nums[j] != j) return j; // the first slot without its own number
}
return n;
}
}C++
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = static_cast<int>(nums.size());
int i = 0;
while (i < n) {
int v = nums[i];
if (v < n && nums[v] != v) { // v belongs at index v; send it there
swap(nums[i], nums[v]);
} else {
i++; // v is home already, or is n, which has no slot
}
}
for (int j = 0; j < n; j++) {
if (nums[j] != j) return j; // the first slot without its own number
}
return n;
}
};Python
class Solution:
def missingNumber(self, nums: list[int]) -> int:
n = len(nums)
i = 0
while i < n:
v = nums[i]
if v < n and nums[v] != v: # v belongs at index v; send it there
nums[i], nums[v] = nums[v], v
else:
i += 1 # v is home already, or is n, which has no slot
for j in range(n):
if nums[j] != j:
return j # the first slot without its own number
return nCommon mistakes
- Trying to send
nto indexn, which is past the end. - Moving
ion right after a swap. The value that just arrived must be looked at too. - Using the sum method with 32-bit numbers when
nis large enough forn(n + 1)/2to overflow.