Missing Number
Find the missing number in a list that holds 0 to n
DSA, Easy, Bit Manipulation
Which structure or algorithm, and what does it cost.
A raffle sold tickets numbered 0 to n, and one ticket was lost. The list nums holds the n ticket numbers you still have, all different, in no set order. Return the number of the lost ticket.
Examples
Constraints
- 1 <= length of
nums<= 20,000. Call the length n. - Each value is a whole number from 0 to n, and no value appears twice.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
You know exactly which numbers should exist, 0 up to n. Think about how to compare that full set with what you hold.
Hint 2 the approach
Looking up each number from 0 to n in the list is slow. Look for a way to cancel every number you hold against its expected twin.
Hint 3 the key step
Xor cancels equal numbers. If you xor every number that should exist with every number you hold, which number has no twin? Check that n itself is in the pile.
Walkthrough
Full walkthrough brute force to best
Brute force
For each number from 0 to n, scan the list to see whether it is there, and return the first one that is not. Each scan can run the whole list, so this is O(n^2) time and O(1) memory. For 20,000 values that is up to about 400 million checks.
Better method
Put every value in a set, then walk 0 to n and return the first number the set does not hold. This is O(n) time, but the set needs O(n) memory.
Use the total. The numbers 0 to n add up to n times n plus 1, divided by 2, so the answer is that total minus the sum of the list. This is O(n) time and O(1) memory. With n up to 20,000 the total is about 200 million, so it fits in 32 bits here. For a much larger n the sum can overflow, and xor never can.
Best method
Xor the numbers 0 to n together with every value in the list. Every number that is in the list appears twice in that pile, once from the range and once from the list, and a pair cancels to 0. The lost number appears only once, from the range, so it is what is left. Xor never grows past the size of its inputs, so there is no overflow. Start the running value at n, then xor in each position i and the value at that position. That covers the whole range 0 to n.
Hand trace
Run on [5, 2, 0, 3, 1], where n is 5. Start with 5. Position 0, value 5: xor with 0 and 5, the running value is 0. Position 1, value 2: xor with 1 and 2, the running value is 3. Position 2, value 0: xor with 2 and 0, the running value is 1. Position 3, value 3: xor with 3 and 3, the running value is 1. Position 4, value 1: xor with 4 and 1, the running value is 4. Return 4.
Code
def missing_number(nums):
result = len(nums) # the number n
for i, value in enumerate(nums):
result ^= i ^ value
return resultTime and memory
O(n) time, because it makes one pass. O(1) memory, because it keeps one running value.
Common mistakes
- Starting the running value at 0 and xoring only the positions 0 to n minus 1. The number n is never covered, so [0] would wrongly give 0.
- Assuming the range starts at 1. Here 0 is a real ticket, and [1, 2, 3] must give 0.
- Using a set and calling the memory constant. The set grows with the list.