← Whiteboard questions

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

Input nums = [5, 2, 0, 3, 1]
Output 4
Why n is 5, so tickets 0 to 5 exist. Only 4 is not in the list.
Input nums = [0]
Output 1
Why n is 1, so tickets 0 and 1 exist. You hold ticket 0, so ticket 1 is lost.
Input nums = [1, 2, 3]
Output 0
Why n is 3. The lost ticket can be 0, the smallest number in the range.

Constraints

Your answer must contain

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 result

Time 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.

Related problems

More Bit Manipulation problems
Missing Number: hints, walkthrough and Python solution