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:
true
💡 Note:
We can form two groups: [1,2,3] and [5,6,7]. Both groups have 3 consecutive cards.
Constraints
- 1 ≤ hand.length ≤ 104
- 0 ≤ hand[i] ≤ 109
- 1 ≤ groupSize ≤ hand.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code