Given an array nums containing n integers in the range [0, n] without any duplicates, return the single number in the range that is missing from nums.
Follow-up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
Example 1:
Input: nums = [1,2,3]
Output: 0Explanation: Since there are 3 numbers, the range is [0,3]. The missing number is 0 since it does not appear in nums.
Example 2:
Input: nums = [0,2]
Output: 1Constraints:
1 <= nums.length <= 1000
You should aim for a solution with O(n) time and O(1) space, where n is the size of the input array.
A brute force approach would iterate through the range of numbers from 0 to n, checking if each number is present in the given array. If a number is missing, it is returned. This results in an O(n^2) solution. Can you think of a better way? Maybe a data structure could help optimize this process.
We can use a hash set by inserting the given array elements into it. Then, we iterate through the range of numbers from 0 to n and use the hash set for O(1) lookups to find the missing number. Can you think of a way to further optimize this? Maybe a bitwise operator could be useful.
We can use bitwise XOR. When two identical numbers are XORed, the result is 0. Using this property, we can efficiently find the missing number.
We first compute the bitwise XOR of numbers from 0 to n. Then, we iterate through the array and XOR its elements as well. The missing number remains in the final XOR result since all other numbers appear twice—once in the range and once in the array—while the missing number is XORed only once.
Before attempting this problem, you should be comfortable with:
We are given an array containing n distinct numbers taken from the range [0, n].
Exactly one number is missing, and we need to find it.
A simple way to reason about this is:
i should be exactly iSorting the array puts the numbers in order, making this comparison straightforward and beginner-friendly.
0 to n - 1:nums[i] != i, then i is the missing number → return i.n → return n as the result.We are given n distinct numbers taken from the range [0, n], with exactly one number missing.
A natural way to approach this is to ask:
“Can we quickly check whether a number exists in the array?”
Using a hash-based data structure (like a hash set) allows us to:
Once all numbers are stored, we simply look for the number in the range [0, n] that does not appear in the set.
This approach trades a little extra space for very clear and simple logic.
0 to n:We are given n distinct numbers from the range [0, n], with exactly one number missing.
A very powerful observation comes from the properties of XOR (⊕):
a ⊕ a = 0 (a number cancels itself)a ⊕ 0 = aIf we XOR:
0 to nEvery number that appears in both places will cancel out, leaving only the missing number.
This allows us to find the answer in linear time and constant space, without sorting or extra data structures.
n be the length of the array.xorr with n.i from 0 to n - 1:xorr with ixorr with nums[i]xorr will contain the missing number.xorr.We are given n distinct numbers from the range [0, n], with exactly one number missing.
A simple mathematical observation helps here:
0 to n is knownInstead of computing two separate sums, we can combine both ideas into a single running calculation, which keeps the logic clean and avoids overflow issues in some languages.
This approach uses basic arithmetic, making it easy to understand and language-independent.
n be the length of the array.res = n.i from 0 to n - 1:i to resnums[i] from resres will hold the missing number.res as the answer.The range is [0, n] for an array of length n, meaning n itself could be missing. Solutions that only check indices 0 to n-1 will miss this case and fail to return n when all other numbers are present.
When using the mathematical approach, computing the full expected sum n * (n + 1) / 2 separately before subtracting can cause overflow for large n. Combining addition and subtraction in a single loop avoids this issue.