Hand of Straights - Problem

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

Given an integer array hand where hand[i] is the value written on the i-th card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

Note: Each card can only be used once, and all cards must be used to form complete groups.

Input & Output

Example 1 — Basic Valid Case
$ Input: hand = [1,2,3,6,2,3], groupSize = 3
Output: true
💡 Note: We can form two groups: [1,2,3] and [2,3,6]. Both groups have 3 consecutive cards.
Example 2 — Invalid Case
$ Input: hand = [1,2,3,4,5], groupSize = 4
Output: false
💡 Note: We have 5 cards but need groups of 4. Since 5 is not divisible by 4, we cannot form complete groups.
Example 3 — Gap in Sequence
$ Input: hand = [1,2,3,5,6,7], groupSize = 3
Output: false
💡 Note: We have [1,2,3] and [5,6,7]. There's a gap between 3 and 5, so we can't form groups with the available cards consecutively.

Constraints

  • 1 ≤ hand.length ≤ 104
  • 0 ≤ hand[i] ≤ 109
  • 1 ≤ groupSize ≤ hand.length

Visualization

Tap to expand
Hand of Straights: Group Formation123623Input: [1,2,3,6,2,3], groupSize = 3Group 1: [1, 2, 3]Group 2: [2, 3, 6]✓ Consecutive✓ ConsecutiveResult: trueAll cards successfully grouped into consecutive sequences
Understanding the Visualization
1
Input Analysis
Cards [1,2,3,6,2,3] with groupSize=3
2
Group Formation
Form consecutive groups of specified size
3
Validation
Check if all cards are used
Key Takeaway
🎯 Key Insight: Always start forming groups from the smallest available card to ensure optimal resource utilization
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
89.0K Views
Medium Frequency
~15 min Avg. Time
2.2K 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