Math Fundamentals18 sections · 814 units
Open in Course

Consecutive - Core Idea 1

Start of sequence

A consecutive sequence has a starting number. For [1,2,3,4][1, 2, 3, 4], the start is 11. You know xx is a start if x1x - 1 does not exist in the array.

For each potential start, count how many consecutive numbers follow it. Use a set for O(1)O(1) membership checks.

This avoids counting the same sequence multiple times. You only start counting from the beginning of each sequence.