228. Hand of Straights

You are given an array of integers hand, where hand[i] is the value on the ith card that Alice owns.

Alice wants to split her entire hand into groups such that:

  • every group contains exactly groupSize cards, and
  • the card values in each group form a sequence of groupSize consecutive integers (e.g. [3, 4, 5], [10, 11, 12, 13]).

Return true if such a rearrangement is possible; otherwise return false.

Example 1:

Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3

Output: true

Explanation:

One possible partition is [1,2,3] [2,3,4] [6,7,8].

Example 2:

Input: hand = [1,2,3,4,5], groupSize = 4

Output: false

Explanation:

There is no way to split the hand into groups of 4 consecutive cards.

Now Your Turn!

Pick the correct output for the given input

Input: hand = [9,9,9], groupSize = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Fun Facts

0
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
// Your code goes here
}
};
Test Case

Input:

Group Size
Hand