Determine whether a multiset of cards can be rearranged into groups of equal size where each group contains consecutive values.
You are given an array of integers representing cards in hand and an integer groupSize. Each card value may appear multiple times.
Rearrange the cards into groups so that:
- Every group has exactly
groupSizecards. - The cards in each group form a sequence of consecutive integers.
- Every card must belong to exactly one group.
Return true if such a rearrangement is possible, otherwise return false.
This is a classic grouping problem where the order of the input does not matter, but the availability of each value does.
Input Format
hand: an array of integers representing card values.groupSize: an integer specifying the required size of each group.
Output Format
Return a boolean indicating whether the cards can be partitioned into valid consecutive groups.
Constraints
1 <= hand.length <= $10^{4}$0 <= hand[i] <= $10^{9}$1 <= groupSize <= hand.length- The same card value may appear multiple times.
Example 1
Input
hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output
true
Explanation
One valid partition is [1,2,3], [2,3,4], and [6,7,8].
Example 2
Input
hand = [1,2,3,4,5], groupSize = 4
Output
false
Explanation
There are 5 cards, which cannot be split into groups of 4 cards each.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.