Skip to main content

Problem Statement

Abstractum

Core Kernel Logic

The Fundamental Principle: Greedy Sequence Extension

The consecutive subsequence problem is fundamentally about greedy decision-making: when processing each number, prefer extending existing sequences over starting new ones, because extension preserves future flexibility while new sequences consume resources.

The Kernel:

  • Process numbers in sorted order (left to right)
  • At each number, face a decision: extend existing sequence OR start new sequence
  • Greedy choice: always prefer extending when possible
  • Track two states: sequences that can still grow (tails) and sequences that are complete (completedSequences)

The Implementation Pattern:

For each number N:
if (canExtend(N)): // Is there a sequence ending at N-1?
extend(N) // Remove N-1 tail, add N tail
else if (canFormNew(N)): // Can we form [N, N+1, N+2]?
formNew(N) // Consume N, N+1, N+2
else:
return "fail" // Can't extend, can't start new

Why Greedy Works:

  • Extending a sequence from length k to k+1 is always better than leaving it at k and starting a new sequence
  • Starting a new sequence requires consuming 3 consecutive numbers immediately
  • Extension preserves flexibility: a sequence ending at 5 can accept 6, 7, 8... indefinitely
  • If we can't extend and can't start new, no future number will help (sorted order guarantees this)

The Pattern: When local optimal choices (extend when possible) lead to global optimal solution (all numbers allocated), use greedy algorithm. No need for dynamic programming or backtracking.


Consecutive Subsequence Splitting

Given: A sorted array of integers (e.g., [1, 2, 3, 3, 4, 5])

Task: Determine if you can split the array into one or more groups where:

  1. Each group contains consecutive numbers (like 1→2→3 or 3→4→5)
  2. Each group has at least 3 numbers
  3. Each number from the original array is used exactly once

Return:

  • "pass" if such a split is possible
  • "fail" if it's impossible

Example

Input: [1, 2, 3, 3, 4, 5]
Output: "pass"

Explanation: You can split into:
- [1, 2, 3]
- [3, 4, 5]

Constraints

  • The array is sorted in ascending order
  • Each element can only be used once
  • Subsequences must be consecutive (e.g., 1, 2, 3)
  • Minimum subsequence length is 3

Test Cases

Valid Cases

[1,2,3,3,4,5] → "pass"
[1,2,3,4,5] → "pass"

Invalid Cases

[1,2,3,4,4,5] → "fail" (can't form valid subsequences)
[1,2,4,5] → "fail" (missing 3, not consecutive)