PROBLEM 74 OF 75

Missing Number

Find the one missing value from the range 0..n.

PATTERNBit manipulationWrite the bit truth table before the code.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find the one missing value from the range 0..n.

WHY THIS FITS

Equal values cancel under XOR, leaving only the missing value.

COMPLEXITY

O(n) time · O(1) space

COMMON FAILURE

Summation can overflow in fixed-width languages.

THE REASONING, IN ONE PLACE

How to solve Missing Number

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Find the one missing value from the range 0..n.

Why Bit manipulation fits

Equal values cancel under XOR, leaving only the missing value.

State to maintain

An accumulator initialized to n.

Transition

XOR every index and value into the accumulator.

Time and space

O(n) time · O(1) space

Counterexample to the tempting mistake

Summation can overflow in fixed-width languages.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗