> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# X of a Kind in a Deck of Cards Python Solution

> Tested Python solution for LeetCode 914 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 914, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Counting](/catalog/topics/counting), [Number Theory](/catalog/topics/number-theory), Euclidean Algorithm, Greatest Common Divisor. [View on LeetCode](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 914   # by problem number
lcpy gen -s x_of_a_kind_in_a_deck_of_cards   # by problem name
```

## Problem

You are given an integer array `deck` where `deck[i]` represents the number written on the `i^th` 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.

### Examples

```
Input: deck = [1,2,3,4,4,3,2,1]
Output: true
```

**Explanation:** Possible partition `[1,1],[2,2],[3,3],[4,4]`.

```
Input: deck = [1,1,1,2,2,2,3,3]
Output: false
```

**Explanation:** No possible partition.

### Constraints

* 1 \<= deck.length \<= 10^4
* 0 \<= deck\[i] \< 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/x_of_a_kind_in_a_deck_of_cards/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/x_of_a_kind_in_a_deck_of_cards/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter
from functools import reduce
from math import gcd


class Solution:
    # Time: O(n + k log m) where k is distinct values, m is max count
    # Space: O(k)
    def has_group_size_x(self, deck: list[int]) -> bool:
        counts = list(Counter(deck).values())
        return reduce(gcd, counts) > 1
```

## Complexity

| Time | Space |
| - | - |
| O(n + k log m) where k is distinct values, m is max count | O(k) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.