X of a Kind in a Deck of Cards - Problem

You have a deck of cards, and each card has a number written on it. Your task is to determine if you can partition all the cards into groups where:

  • Each group has exactly X cards (where X โ‰ฅ 2)
  • All cards in the same group have the same number
  • Every card must be placed in exactly one group

Return true if such a partition is possible, false otherwise.

Example: If you have cards [1,2,3,4,4,3,2,1], you could group them as:

  • Group 1: [1,1] (2 cards with value 1)
  • Group 2: [2,2] (2 cards with value 2)
  • Group 3: [3,3] (2 cards with value 3)
  • Group 4: [4,4] (2 cards with value 4)

Since all groups have the same size (2) and X=2 โ‰ฅ 2, this partition is valid!

Input & Output

example_1.py โ€” Basic valid partition
$ Input: [1,2,3,4,4,3,2,1]
โ€บ Output: true
๐Ÿ’ก Note: We can partition into groups of size 2: [1,1], [2,2], [3,3], [4,4]. Each group has exactly 2 cards with the same value.
example_2.py โ€” No valid partition
$ Input: [1,1,1,2,2,2,3,3]
โ€บ Output: false
๐Ÿ’ก Note: We have 3 ones, 3 twos, and 2 threes. No single group size X works for all: X=2 doesn't divide 3, X=3 doesn't divide 2.
example_3.py โ€” Single card type
$ Input: [1,1,1,1,1,1]
โ€บ Output: true
๐Ÿ’ก Note: All 6 cards have value 1. We can form 3 groups of size 2, or 2 groups of size 3, or 1 group of size 6. All are valid since X โ‰ฅ 2.

Visualization

Tap to expand
๐ŸŽฏ Card Grouping Tournament VisualizationExample: deck = [1,1,1,1,2,2,2,2,2,2]Card Value 11111Frequency: 4Can form 2 groups of size 2Card Value 2222222Frequency: 6Can form 3 groups of size 2GCD CalculationGCD(4, 6) = ?4 = 2ยฒ ร— 16 = 2ยน ร— 3ยนGCD = 2Final Grouping (Group Size = 2)Group 1: [1,1]Group 2: [1,1]Group 3: [2,2]Group 4: [2,2]Group 5: [2,2]โœ“ Valid!Why GCD Works: Mathematical InsightIf we can partition into groups of size X, then X must divide every frequencyExample: freq=[4,6], possible X values are common divisors: 1, 2Since X โ‰ฅ 2, we need the largest common divisor โ‰ฅ 2GCD(4,6) = 2 โ‰ฅ 2 โœ“ โ†’ Answer: true๐ŸŽฏ **Key Insight:** GCD of all frequencies gives us the largest valid group size!
Understanding the Visualization
1
Count Players by Card
Count how many players have each card value - this gives us frequency constraints
2
Find Team Size
The team size must divide evenly into every card group - this is where GCD helps
3
Verify Minimum
Check if the largest valid team size is at least 2 players
4
Form Teams
If GCD โ‰ฅ 2, we can successfully partition all players into equal-sized teams
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to finding GCD of frequencies. If GCD โ‰ฅ 2, we can partition all cards into equal-sized groups where each group contains cards of the same value.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log(max_freq))

Single pass O(n), with GCD computation O(log(max_freq)) for each unique card

n
2n
โšก Linearithmic
Space Complexity
O(k)

Hash map to store frequency of k unique card values

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค deck.length โ‰ค 104
  • 0 โ‰ค deck[i] < 104
  • Group size must be at least 2
  • All cards must be partitioned (no leftover cards allowed)
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 19
42.4K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen