Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Hand of Straights

Problem in Plain English

Given card values and a group size, decide whether the hand splits into groups of that size where each group holds consecutive values. Every card must be used exactly once.

Problem Statement

Alice holds cards with values hand[i] and wants groups of exactly groupSize cards where each group is a run of consecutive values. Return true when the whole hand splits into such groups, and false otherwise.

For hand = [1, 2, 3, 6, 2, 3, 4, 7, 8] with groupSize = 3 the answer is true via [1,2,3], [2,3,4], and [6,7,8]. For hand = [1, 2, 3, 4, 5] with groupSize = 4 the answer is false because five cards cannot fill groups of four.

Constraints

  • 1 <= hand.length <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= groupSize <= hand.length

Examples

Example 1

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

The hand splits into [1,2,3], [2,3,4], and [6,7,8].

Example 2

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

Each group would use four cards, but five is not a multiple of four. One card would remain regardless of the arrangement, so return false.

Example 3

Input
hand = [1, 1, 2, 2, 3, 4], groupSize = 3
Output
false

The size is divisible by three, but both copies of the smallest value 1 must start [1,2,3]. There are two copies of 2 and only one 3, so the second group cannot be completed. The 4 cannot replace the missing 3.

Example 4

Input
hand = [0], groupSize = 1
Output
true

A group of size one is already a consecutive run. The single card forms [0], so every card is used exactly once.

Intuition

Imagine laying the remaining cards in sorted order. The smallest remaining value cannot appear after another card in a group: that would require an even smaller remaining value. It must start a run. In Example 1, the only 1 forces [1,2,3]; the remaining 2 then forces [2,3,4]. Duplicate values are separate cards, so overlapping runs are allowed but reusing a card is not. This forced-start rule lets us make a greedy choice without guessing between partitions.

Approaches

Sort And Rescan

Solution Details

Reveal Sort And Rescan: intuition, complexity, and code

Hints

Hint 1
Ask which card the smallest remaining card must sit beside in its group.
Hint 2
Count copies of each value so repeats stop being special cases.
Hint 3
Always open the next group at the current minimum and demand the following consecutive values in the same quantity.

Edge Cases

  • A nondivisible length fails before sorting or allocating grouping state, such as five cards with groups of four.
  • groupSize = 1 always succeeds; [0] is the smallest allowed hand.
  • Repeated complete runs such as [1,1,2,2,3,3] with size three succeed by creating two groups at once in the count sweep.
  • Divisibility alone is insufficient: [1,1,2,2,3,4] with size three lacks the second 3.
  • Gaps between separate completed groups are allowed: [1,2,7,8] with size two succeeds; a gap inside a forced group fails.
  • Values near 10^9 still require exact successors. A hand of repeated 10^9 values fails for size two; there is no wraparound successor.

Common Mistakes and Interview Tips

  • Treating duplicate values as one card loses multiplicity; [1,1,2,2,3,3] needs two copies of every member.
  • Using the first group's starting count directly after decrementing it makes need become zero. Snapshot need before consuming any member.
  • Grouping adjacent chunks of the sorted array fails when valid groups overlap: Example 1 needs two overlapping runs, not the first sorted chunk [1,2,2].
  • Using JavaScript's default lexicographic sort visits 10 before 2; always supply (a, b) => a - b.
  • Returning true after checking only the length misses frequency shortages. Every forced successor must actually exist in sufficient quantity.
  • Calling an in-place sort on the caller's hand unexpectedly mutates input. Sort a clone or a fresh list of keys as these snippets do.

Key Takeaway

When items must form consecutive runs, the remaining minimum dictates the next group, so count frequencies and build upward from it.