Math Fundamentals18 sections · 814 units
Open in Course

Consecutive - Core Idea 2

Counting forward

Once you identify a start, count forward. Check if x+1x + 1, x+2x + 2, x+3x + 3, and so on exist in the set. Stop when you find a gap.

For each start, this takes O(k)O(k) time where kk is the length of the sequence. But each number is visited at most twice (once as a candidate start, once during counting), so total time is O(n)O(n).

Track the maximum sequence length found. That is your answer.