Skip to content

950. X Of A Kind In A Deck Of Cards

Array Hash Table Math Counting Number Theory

Problem - X Of A Kind In A Deck Of Cards

Easy

You are given an integer array deck where deck[i] represents the number written on the ith card.

Partition the cards into one or more groups such that:

  • Each group has exactly x cards where x > 1, and
  • All the cards in one group have the same integer written on them.

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

 

Example 1:

Input: deck = [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].

Example 2:

Input: deck = [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.

 

Constraints:

  • 1 <= deck.length <= 104
  • 0 <= deck[i] < 104

Solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def hasGroupsSizeX(self, deck: List[int]) -> bool:
        # count = Counter(deck)
        # n = len(deck)
        # for i in range(2, n + 1):
        #     if n % i == 0:
        #         if all(val % i == 0 for val in count.values()):
        #             return True
        # return False
        count = Counter(deck)
        return reduce(gcd, count.values()) >= 2

Submission Stats:

  • Runtime: 3 ms (76.69%)
  • Memory: 18.4 MB (6.85%)