Problem overview
The method missingNumber receives a vector<int> named nums with n distinct numbers, each between 0 and n. The range 0 to n holds n + 1 numbers, so exactly one is absent. The method returns it.
For nums = [3, 0, 1], n is 3 and the range is 0, 1, 2, 3. Only 2 is absent, so the answer is 2. For [0, 1] the answer is 2, which is n itself, and for [1] it is 0.
The statement asks for O(n) time and O(1) extra space.
Approach: let equal numbers cancel with XOR
There are easier ways that cost more. Sorting works but takes O(n log n). A set of seen values is O(n) time but O(n) space. The sum formula n * (n + 1) / 2 minus the sum of nums also works for these limits, but XOR is the idea this lesson practices.
XOR has three properties: x ^ x = 0, x ^ 0 = x, and the order of operands doesn't matter. So if every number but one appears twice in a long XOR, the result is the number that appears once.
Here, no value repeats inside nums, but we can give each of them a partner. The indices 0 to n - 1 plus the number n are exactly all the numbers from 0 to n, once each. XOR all of those together with all the values in nums. Every value present in nums appears twice, once as an index (or as n) and once as a value, and cancels. The missing number appears only among the indices and n, so it survives.
For [3, 0, 1], starting at result = 3, the lesson's code gives result as 0 after index 0, 1 after index 1, and 2 after index 2. That final 2 is the answer.
Step by step
- Start
resultatnums.size(), which isn. The indices stop atn - 1, sonhas to enter the XOR here. - Loop over every index from 0 to
nums.size() - 1. - XOR in the index and the value:
result ^= i ^ nums[i]. - Return
result. Every present number cancelled, and the missing one is left.
The solution in C++
class Solution {
public:
int missingNumber(vector<int>& nums) {
int result = nums.size();
for (int i = 0; i < nums.size(); i++) {
result ^= i ^ nums[i];
}
return result;
}
};
C++ details worth knowing:
size()is unsigned.nums.size()returnssize_t. Assigning it to anintis fine for sizes up to 100,000. Comparingint iwith it convertsito unsigned, which is harmless whileiis never negative (some compilers warn about the mix).^=is XOR-assign:result ^= xmeansresult = result ^ x. Herexis the whole expressioni ^ nums[i], which is evaluated first.- By reference.
vector<int>& numsavoids copying the vector. - No overflow. XOR never produces a value larger than the bit width of its operands, so unlike the sum formula it cannot overflow.
Complexity
Time: O(n). One pass, with two XORs per element.
Space: O(1). A single integer, result, plus the loop variable.
Edge cases and common mistakes
- The missing number is
n. For[0, 1]every index and value cancels except the initial 2. Startingresultat 0 would return 0, which is wrong. - The missing number is 0. For
[1]the result is 0, which is a valid answer, not an error signal. - A single element.
[0]gives 1, and[1]gives 0. - Unsorted input. The order of
numsdoesn't matter, because XOR doesn't care about order. For[9, 6, 4, 2, 3, 5, 7, 0, 1]the answer is 8. - Confusing index and value. Both go in: XORing only the values leaves the XOR of the whole input, not the missing number.
Testing
[3, 0, 1]gives 2.[0, 1]gives 2.[1]gives 0.[9, 6, 4, 2, 3, 5, 7, 0, 1]gives 8.
