2Brute2Best
πŸ”₯0

Hand of Straights

MediumΒ·LeetCode #846 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Alice has a hand of cards. Each card shows a number. She wants to split all of her cards into groups of exactly groupSize cards, where the numbers in each group are consecutive, like 3, 4, 5.

Return true if she can do this, and false if not.

Input

An array hand of card values, and an integer groupSize.

Output

true if the cards can be split into groups of consecutive values, otherwise false.

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

Split into [1,2,3], [2,3,4] and [6,7,8].

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

Five cards can't be split into groups of four.

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

Two copies of 3 are fine: [1,2,3] and [3,4,5].

Constraints
  • β€’ 1 ≀ hand.length ≀ 10^4
  • β€’ 0 ≀ hand[i] ≀ 10^9
  • β€’ 1 ≀ groupSize ≀ hand.length
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œgroups of consecutive cards”
β†’ A group is a run of values, so its start value decides the whole group.
β€œrearrange into groups of groupSize”
β†’ Every card must be used, so the copies of each value matter.
β€œreturn true or false”
β†’ Any one valid split is enough. We never need to report the split itself.
03

Approach Complexity Ladder

Brute Force β€” try every choice of group startO(N^(N/G))O(N)

Backtrack: try every card value as the start of the next group, and undo when a choice fails

Optimal β€” ordered map of counts, always start from the smallest cardO(N log N)O(N)

Ordered map of counts; the smallest remaining card starts each group, which takes groupSize consecutive values

Brute Force Intuition

Pick any unused card as the start of the next group. The group is that value and the next groupSize βˆ’ 1 values. Take those cards out, and recurse on what is left. If the rest fails, put the cards back and try another start.

Recognition rule for this sub-pattern:

The best choice changes as you go β€” keep candidates in a heap and always pop the current best.
Remember this Β· Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.