Split Array into Consecutive Subsequences - Problem

Imagine you have a sorted array of integers and need to organize them into consecutive sequences like a card player arranging cards into runs. Your goal is to determine if you can split the entire array into one or more subsequences where:

  • Each subsequence contains consecutive integers (each number is exactly one more than the previous)
  • Every subsequence has at least 3 elements

For example, the array [1,2,3,3,4,5] can be split into [1,2,3] and [3,4,5] - both are consecutive sequences of length 3 or more!

The Challenge: Given a sorted array nums, return true if such a split is possible, false otherwise.

Key Insight: This is a greedy problem disguised as a splitting problem. The trick is to always try to extend existing sequences before starting new ones!

Input & Output

example_1.py โ€” Basic Valid Split
$ Input: [1,2,3,3,4,5]
โ€บ Output: true
๐Ÿ’ก Note: We can split this into [1,2,3] and [3,4,5]. Both subsequences are consecutive and have length โ‰ฅ 3.
example_2.py โ€” Invalid Split
$ Input: [1,2,3,3,4,4,5,5]
โ€บ Output: true
๐Ÿ’ก Note: We can form [1,2,3,4,5] and [3,4,5]. The key insight is we can extend the first sequence to length 5.
example_3.py โ€” Cannot Form Valid Split
$ Input: [1,2,3,4,4,5]
โ€บ Output: false
๐Ÿ’ก Note: After forming [1,2,3,4], we have [4,5] left, but this has length < 3, so it's invalid.

Visualization

Tap to expand
๐Ÿƒ Card Player's Greedy StrategyAvailable Cards123345Incomplete Runs[1,2,3] needs 4[3,4,5] needs 6Always extend these first!Greedy Decision Process1. Can I extend existing run? โ†’ YES, do it!2. Can I start new 3-card run? โ†’ YES, do it!3. Neither possible? โ†’ IMPOSSIBLE4. Priority: Extension > New runStep-by-Step Processing1Start [1,2,3]2Continue [1,2,3]3Complete [1,2,3]3Start [3,4,5]4Continue [3,4,5]5Complete [3,4,5]๐ŸŽฏ Key InsightGreedy works because extending sequences is always better than starting new ones!Short incomplete runs are harder to complete later, so prioritize them first.
Understanding the Visualization
1
Count Your Cards
First, count how many of each card value you have
2
Track Incomplete Runs
Keep track of which runs need specific cards to continue
3
Greedy Extension
For each card, always extend existing runs first
4
Start New Runs
Only start new 3-card runs if you can't extend existing ones
Key Takeaway
๐ŸŽฏ Key Insight: The greedy strategy works because extending existing sequences maximizes our chances of using all numbers, since incomplete short sequences are harder to complete later!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through unique numbers, each hash map operation is O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Two hash maps storing at most n unique numbers

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • -1000 โ‰ค nums[i] โ‰ค 1000
  • nums is sorted in non-decreasing order
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
127.5K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen