Problem overview
The function missingNumber receives an IntArray named nums with n distinct numbers, each between 0 and n. The range 0 to n holds n + 1 numbers, so exactly one of them is absent. The function returns it.
For nums = [3, 0, 1], n is 3, so the numbers 0, 1, 2 and 3 should all appear. Only 2 is absent, so the answer is 2. The missing number can also be n itself, as in [0, 1] where the answer is 2, or 0, as in [1] where the answer is 0.
Approach: let XOR cancel the pairs
Sorting or a hash set would find the missing number, but the statement asks for O(n) time and O(1) extra space. A sum of 0 to n minus the sum of the array also works. This lesson uses XOR instead.
XOR has three properties that matter here: x xor x is 0, x xor 0 is x, and the order of the operands doesn't matter. So when a collection of numbers is XOR-ed together, every number that appears twice disappears, and a number that appears once is what is left.
Here no value repeats, so we have to create the partners. The indices of nums run from 0 to n - 1. Add n, and the indices together with n contain every number from 0 to n exactly once. The values in nums contain the same numbers except the missing one. XOR all of them together, and each number present in nums meets its twin among the indices and cancels. The missing number has no twin among the values, so it is the only one left.
Step by step
- Start
resultatnums.size, declared withvar. This isn, which the indices never reach, so it goes in at the start. - Loop
iovernums.indices, the range 0 ton - 1. - XOR in both the index and the value:
result = result xor i xor nums[i]. - Return
result.
The solution in Kotlin
class Solution {
fun missingNumber(nums: IntArray): Int {
var result = nums.size
for (i in nums.indices) {
result = result xor i xor nums[i]
}
return result
}
}
For nums = [3, 0, 1], result starts at 3 and changes like this:
start result=3
i=0 nums[i]=3 result=0
i=1 nums[i]=0 result=1
i=2 nums[i]=1 result=2
The final value, 2, is the missing number.
The Kotlin details worth knowing:
- XOR is
xor. Kotlin has no^operator.xoris an infix function onInt, and chaininga xor b xor cevaluates from left to right. nums.indicesis anIntRangefrom 0 tonums.size - 1. It is the idiomatic way to loop over positions without writing the bounds.- No overflow. XOR never produces a bit that neither operand has, so it can't overflow. A solution that adds the numbers 0 to
ncould: forn = 100000the sum is 5,000,050,000, which doesn't fit in anInt, so it would need aLong.
Complexity
Time: O(n). The loop visits each index once and does two XORs per index.
Space: O(1). The function keeps a single number.
Edge cases and common mistakes
- The missing number is
n. For[0, 1],resultstarts at 2, and the loop cancels 0 and 1. The 2 is never cancelled, so the answer is 2. This is whyresultstarts atnums.sizerather than 0. - The missing number is 0. For
[1], the loop XORs 0 and 1 into the starting 1 and leaves 0. - Starting at 0. Then
nis never cancelled, and the function returns the missing number XORninstead of the missing number itself. - Using
ororandinstead ofxor. Only XOR cancels a number against itself. - A single element.
[0]returns 1, becausen = 1is the missing number.
Testing
nums = [3, 0, 1]gives2.nums = [0, 1]gives2.nums = [1]gives0.nums = [0]gives1.nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]gives8.
