Problem overview
The method missingNumber(nums: Array[Int]): Int receives n distinct numbers, each between 0 and n. The range 0 to n holds n + 1 numbers, so exactly one of them is absent from nums. The method returns it.
For [3, 0, 1], n is 3 and the numbers should be 0, 1, 2 and 3. The array has 0, 1 and 3, so the answer is 2. For [0, 1] the missing number is n itself, 2, and for [1] it is 0.
Approach: give every value a partner
A set of the values, or a sum formula, would also find the answer. The statement asks for O(n) time and O(1) space, and XOR does it with one number of state and no risk of overflow.
XOR has three properties that matter here: x ^ x = 0, x ^ 0 = x, and the order of the operands doesn't matter. So XORing a collection of numbers cancels every number that appears twice and leaves the one that appears once.
In this problem no value repeats, but we can make partners. The indices of nums are 0 to n - 1, and adding n gives every number from 0 to n exactly once. XOR all of those together with all the values in nums. Each value in nums appears twice (once as a value, once among the indices and n) and cancels. The missing number appears only among the indices and n, so it is what remains.
Step by step
- Create
var resultand start it atnums.length, which isn. The indices stop atn - 1, songoes in here. - Loop over every index with
for (i <- nums.indices).nums.indicesis the range 0 ton - 1. - Inside the loop, XOR both the index and the value into
result. - Return
result.
The solution in Scala
class Solution {
def missingNumber(nums: Array[Int]): Int = {
var result = nums.length
for (i <- nums.indices) {
result ^= i ^ nums(i)
}
return result
}
}
Scala details
^is bitwise XOR onInt, and^=is the compound form.result ^= i ^ nums(i)XORs the already combinedi ^ nums(i)intoresult; operator order doesn't change the outcome here, since XOR is associative.nums.indicesgives aRangefrom 0 tonums.length - 1, so there is no off-by-one withuntilorto.nums(i)reads an array element with parentheses, not square brackets.var resultis needed because the loop reassigns it. The parameternumsis never changed.
Tracing [3, 0, 1]
result starts at 3. After each index:
i=0 value=3 result=0
i=1 value=0 result=1
i=2 value=1 result=2
The final value is 2, the missing number. At index 0, 3 ^ 0 ^ 3 is 0, since the 3 from the start and the 3 in the array cancel.
Complexity
Time is O(n): one pass with two XORs per index. Space is O(1): a single integer. Unlike summing 0 + 1 + ... + n, XOR never produces a number larger than the inputs, so there is no overflow to think about.
Edge cases and common mistakes
- The missing number is
n:[0, 1]returns 2. That works only becauseresultstarts atnums.length. Starting at 0 would return 0 here. - The missing number is 0:
[1]returns 0, and the result is correct because XORing with 0 changes nothing. - Single element:
[0]returns 1. - A longer example:
[9, 6, 4, 2, 3, 5, 7, 0, 1]returns 8. - Forgetting one side: XORing only the values, or only the indices, leaves a mix of numbers instead of the answer. Both sides are needed so that the present values cancel.
