Ctrl K

Even Piles of Cards

Easy
Every card in a deck has a number on it, and the deck is given as the array cards. Split the whole deck into piles so that: - every pile holds the same number of cards k, with k ≥ 2; - all cards within a pile show the same number. Every card must go into exactly one pile. Return True if such a split exists, and False otherwise.
Example 1
cards=[5, 2, 5, 2, 2, 5, 2, 5, 5, 5] → True

There are six 5s and four 2s. Both counts divide by 2, giving five piles of 2 cards.

Example 2
cards=[7, 7, 7, 1, 1] → False

There are three 7s and two 1s. No number greater than 1 divides both 3 and 2, so it cannot be done.

Example 3
cards=[4] → False
Constraints
  • 1 ≤ len(cards) ≤ 10⁴
  • 0 ≤ cards[i] < 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCoprime Digit Pairs EasyPhoto Frames of the Same Shape MediumPrimes Along Grid Rays MediumPair GCDs by Sorted Position HardPairs of Equal Values Easy
loading Python…
Input
cards=[5, 2, 5, 2, 2, 5, 2, 5, 5, 5]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.