Neural Mastery
← Back to Practice

The Gap in the Sequence

Difficulty: Medium · Pattern: Cyclic Sort / Array Math · Concept: General Coding (DSA) — Core Patterns to Drill

You're given a list of n distinct integers, each drawn from the range 0 to n inclusive (that's n + 1 possible values), with exactly one value missing. Find the missing one.

The cyclic sort family of problems exploits a strong constraint most array problems don't have: when values are known to fall in a bounded, contiguous range tied to the array's own length, you can reason about "where should this value be" instead of searching or sorting generally. Here that constraint gives an even more direct route: a closed-form sum.

Your task: implement find_missing_number(nums) in O(n)O(n) time and O(1)O(1) extra space (no set, no counting array).

Implement it yourself
assert find_missing_number([3, 0, 1]) == 2 assert find_missing_number([0, 1]) == 2 assert find_missing_number([9, 6, 4, 2, 3, 5, 7, 0, 1]) == 8 assert find_missing_number([0]) == 1

Next: Find All Duplicates in an Array (a harder variant), or continue to Largest Rectangle in a Skyline

Last updated Sep 5, 2026Edit this pageReport an issue