Hand of Straights - Problem
Card Sequence Challenge: Alice has a deck of cards with numbers written on them and wants to organize them into consecutive groups. Each group must contain exactly groupSize cards with consecutive values (like 3,4,5 or 7,8,9,10).

Given an array hand where each element represents a card value, and an integer groupSize, determine if Alice can successfully arrange all her cards into valid consecutive groups.

Example: If Alice has cards [1,2,3,6,2,3,4,7,8] and groupSize = 3, she can form groups [1,2,3], [2,3,4], and [6,7,8] - so return true.

Input & Output

example_1.py โ€” Basic Consecutive Groups
$ Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
โ€บ Output: true
๐Ÿ’ก Note: Alice can form three groups: [1,2,3], [2,3,4], and [6,7,8]. Each group has exactly 3 consecutive cards.
example_2.py โ€” Impossible Grouping
$ Input: hand = [1,2,3,4,5], groupSize = 4
โ€บ Output: false
๐Ÿ’ก Note: We have 5 cards but need groups of size 4. Since 5 is not divisible by 4, it's impossible to form valid groups.
example_3.py โ€” Single Card Groups
$ Input: hand = [1,1,2,2,3,3], groupSize = 1
โ€บ Output: true
๐Ÿ’ก Note: When groupSize is 1, each card forms its own group. Any hand can be arranged this way.

Visualization

Tap to expand
๐Ÿƒ Hand of Straights: Greedy SolutionInput: [1,2,3,6,2,3,4,7,8], groupSize = 3Step 1: Count Frequencies1:1 2:2 3:24:1 6:1 7:1 8:1Step 2: Form Groups Greedily1start2next3endGroup 1: [1,2,3]2start3next4endGroup 2: [2,3,4]6start7next8endGroup 3: [6,7,8]โœ… Result: true - All cards successfully grouped!Algorithm: O(n log n) time, O(n) space๐ŸŽฏ Key InsightAlways start sequences with the smallest available card.This greedy approach guarantees optimal grouping if a solution exists.
Understanding the Visualization
1
Count All Cards
Create a frequency map to know how many of each card value we have
2
Sort Card Values
Process card values in ascending order to ensure optimal grouping
3
Greedy Group Formation
For each card value, form as many consecutive sequences as possible
4
Validate Completeness
Ensure all cards are used and all groups are properly formed
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of always starting with the smallest available card is optimal because it never blocks better arrangements - if a valid grouping exists, the greedy method will find it.

Time & Space Complexity

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

O(n log n) for sorting + O(n log n) for priority queue operations

n
2n
โšก Linearithmic
Space Complexity
O(n)

Priority queue and additional data structures to track sequences

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค hand.length โ‰ค 104
  • 0 โ‰ค hand[i] โ‰ค 109
  • 1 โ‰ค groupSize โ‰ค hand.length
  • All card values are non-negative integers
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.8K Views
Medium-High Frequency
~18 min Avg. Time
1.5K 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