Shortest Impossible Sequence of Rolls - Problem

Imagine you're analyzing dice roll patterns to find the shortest sequence that's guaranteed to never appear in your data!

You have an array rolls of length n representing the results of rolling a k-sided dice (numbered 1 to k) exactly n times. Your task is to find the length of the shortest sequence of consecutive rolls that cannot be found as a subsequence in the given rolls array.

Key insight: A sequence of length len represents the result of rolling the dice len times consecutively. We need to find the minimum len such that there exists at least one sequence of length len that doesn't appear as a subsequence in our rolls.

Example: If rolls = [4,2,1,2,3] and k = 4, we need to find the shortest length where at least one possible sequence of that length is missing from the subsequences of our rolls.

Input & Output

example_1.py โ€” Basic Case
$ Input: rolls = [4,2,1,2,3], k = 4
โ€บ Output: 2
๐Ÿ’ก Note: For length 1: All values 1,2,3,4 appear as subsequences. For length 2: We can form sequences like [4,2], [2,1], [1,2], [2,3], but we cannot form [1,1] or several others. So the answer is 2.
example_2.py โ€” Longer Sequence
$ Input: rolls = [1,1,2,2], k = 2
โ€บ Output: 2
๐Ÿ’ก Note: For length 1: Both [1] and [2] exist. For length 2: We have [1,1], [1,2], [2,2] but missing [2,1]. So answer is 2.
example_3.py โ€” Edge Case
$ Input: rolls = [1,1,1,1], k = 4
โ€บ Output: 1
๐Ÿ’ก Note: Only value 1 appears in rolls, so sequences [2], [3], [4] are all missing. The shortest impossible sequence has length 1.

Visualization

Tap to expand
Recipe Collection VisualizationRecipe Length: 1BasketEmpty: {}Ingredient Stream:4213After ingredient 4:{4}After ingredient 2:{4,2}After ingredient 1:{4,2,1}After ingredient 3:{4,2,1,3}COMPLETE!Recipe of Length 1 Completed!All 4 ingredients collected: {1, 2, 3, 4}Moving to Recipe Length 2, Reset BasketContinue until you can't complete enough recipes of current length
Understanding the Visualization
1
Start Collecting
Begin with an empty ingredient basket and target recipe length 1
2
Add Ingredients
As you see each dice roll (ingredient), add it to your basket
3
Complete Recipe
When you have all k ingredients, you've completed one recipe
4
Level Up
Clear basket, increase recipe length, continue collecting
5
Find Answer
When rolls end, current length is the shortest impossible sequence
Key Takeaway
๐ŸŽฏ Key Insight: The moment you can't collect all k ingredients to complete a recipe, you've found the shortest impossible sequence length!

Time & Space Complexity

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

Single pass through the rolls array, with O(1) set operations

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

Only need to store at most k different values in the set

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค rolls[i] โ‰ค k โ‰ค 105
  • rolls[i] represents the result of rolling a k-sided dice
Asked in
Google 42 Meta 28 Amazon 35 ByteDance 15
41.3K Views
Medium Frequency
~25 min Avg. Time
1.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