Count Number of Special Subsequences - Problem

Imagine you're organizing a three-stage tournament where participants must progress through stages in a specific order: Training (0), Qualifying (1), and Championship (2). A special subsequence represents a valid tournament path that includes at least one participant from each stage, in the correct order.

Given an array nums containing only integers 0, 1, and 2 (representing the three tournament stages), your task is to count how many different subsequences form valid tournament paths.

A special subsequence must follow this pattern:

  • Start with one or more 0s (Training stage)
  • Follow with one or more 1s (Qualifying stage)
  • End with one or more 2s (Championship stage)

Examples of valid paths:

  • [0,1,2] - Basic path through all stages
  • [0,0,1,1,1,2] - Multiple participants at each stage

Invalid paths:

  • [2,1,0] - Wrong order
  • [1] - Missing stages
  • [0,1,2,0] - Going backwards

Return the total count modulo 109 + 7 since the answer can be very large.

Input & Output

example_1.py β€” Basic Special Subsequence
$ Input: nums = [0,1,2]
β€Ί Output: 1
πŸ’‘ Note: The only special subsequence is [0,1,2] itself, following the required pattern of 0+ 1+ 2+
example_2.py β€” Multiple Valid Paths
$ Input: nums = [0,1,2,1,2]
β€Ί Output: 5
πŸ’‘ Note: Valid subsequences are: [0,1,2] (index 0,1,2), [0,1,1,2] (index 0,1,3,4), [0,1,2,2] (index 0,1,2,4), [0,1,1,2] (index 0,1,3,2), [0,1,2,1,2] (all indices). Total count = 5
example_3.py β€” Edge Case
$ Input: nums = [2,1,0]
β€Ί Output: 0
πŸ’‘ Note: No special subsequences possible since the array is in reverse order. We need at least one 0, followed by at least one 1, followed by at least one 2.

Visualization

Tap to expand
Tournament Progression: [0,1,2,1,2]Training (0s)01 wayQualifying (1s)113 waysChampionship (2s)225 waysState EvolutionStep by StepStart: z=0, o=0, t=0See 0: z=1See 1: o=1See 2: t=1See 1: o=3See 2: t=5Key Formulazeros = zerosΓ—2 + 1ones = onesΓ—2 + zerostwos = twosΓ—2 + onesDouble existing + extendValid Paths[0,1,2][0,1,1,2][0,1,2,2][0,1,1,2,2][0,1,2,1,2]Total: 5🎯 Result: 5 valid tournament progression paths found!
Understanding the Visualization
1
Training Stage
Count all possible ways to select training participants (0s)
2
Qualifying Stage
For each training group, count ways to add qualifying participants
3
Championship Stage
For each training+qualifying group, add championship participants
4
Valid Paths
Only complete paths through all three stages count as special subsequences
Key Takeaway
🎯 Key Insight: Each element can either extend existing subsequences (doubling the count) or create new ones by combining with the previous stage, leading to an elegant O(n) solution.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the array with constant work per element

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only three variables needed to track DP states

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ nums.length ≀ 105
  • nums[i] ∈ {0, 1, 2}
  • Return result modulo 109 + 7
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.0K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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