easyBit ManipulationMathArrays & Hashing

Missing Number

easyTime: O(n)Space: O(1)

Recognize the pattern

find the one missing number in 0.nn numbers given, one is missingsummation or XOR

Brute force idea

The naive version of Missing Number sounds like this: Sort and scan for the gap. Or use a boolean array. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

Better approach

The deeper shift in Missing Number is this: Expected sum = n(n+1)/2. Actual sum = sum of array. Missing = expected - actual. Or XOR all numbers with 0.n — the missing number is the result. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

At the center of Missing Number is one steady idea: The sum of 0.n is a known formula. The difference between the expected sum and the actual sum is exactly the missing number. XOR achieves the same by canceling all paired values. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Watch out for

A common way to get lost in Missing Number is this: Integer overflow with the sum approach for very large n — use XOR instead, which never overflows. Or use long/BigInt. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Arrays & Hashing Pattern