Equal-Sized Uniform Piles From a Card Collection
Math & Bit ManipulationAuto-graded · 25 test casesLast updated
Decide whether a multiset of integers can be split into uniform piles that all share one common size of at least two, testing counting and the GCD insight.
You are given a list of integers cards, where each integer is the value printed on a card.
You want to arrange every card into piles so that:
- each pile contains cards of one single value only,
- every pile contains exactly the same number of cards, call it
s, and sis at least 2.
Return True if some choice of s makes this possible, and False otherwise.
Every card must be placed in exactly one pile. Different piles may hold the same value, so a value that appears many times can be split across several piles.
If cards is empty, return False, since no valid pile size can be chosen.
Constraints
0 <= len(cards) <= 10^40 <= cards[i] < 10^4- The return value is a boolean (
TrueorFalse).
Examples
Example 1
Input: can_form_equal_piles([1, 2, 3, 4, 4, 3, 2, 1])
Output: True
Example 2
Input: can_form_equal_piles([1, 1, 1, 2, 2, 2, 3, 3])
Output: False
Example 3
Input: can_form_equal_piles([1])
Output: False
Hints
Hint 1
Look at each distinct value on its own: if a value appears c times, which pile sizes can split exactly c cards into equal piles?
Hint 2
The same pile size s must work for every distinct value, so s must divide every one of the occurrence counts.
Hint 3
A common divisor of at least 2 exists exactly when the greatest common divisor of all the counts is at least 2. Fold gcd across the counts.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Count how many times each value occurs. A pile holds one value only, so for a value occurring c times, the pile size s must divide c exactly. The same s has to work for every distinct value, so s must be a common divisor of all the counts.
A common divisor of at least 2 exists exactly when the greatest common divisor (GCD) of all counts is at least 2. So the algorithm is:
- Count occurrences of each value.
- Fold
gcdover the counts, starting from0(sincegcd(0, a) = a). - Return
Trueif the result is at least 2.
An empty list leaves the fold at 0, which correctly gives False. We can also exit early once the running GCD reaches 1, since it can never grow again.
Why it is correct
- Necessity: if a valid pile size
s >= 2exists, it divides every count, so it is a common divisor. The GCD is the largest common divisor, hencegcd >= s >= 2. - Sufficiency: if
g = gcd(counts) >= 2, then each value withcoccurrences can be split intoc / gpiles of sizeg. All piles then have sizegand contain a single value.
Note that counts do not need to be equal (4 and 6 work with s = 2), and having every count at least 2 is not sufficient (2 and 3 share no divisor above 1).
Complexity
- Time: O(n + k log M), where n is the number of cards, k the number of distinct values, and M the largest count.
- Space: O(k) for the counts.
Reference solution
from collections import Counter
from math import gcd
from typing import List
def can_form_equal_piles(cards: List[int]) -> bool:
counts = Counter(cards).values()
common = 0
for count in counts:
common = gcd(common, count)
if common == 1:
return False
return common >= 2