> ## 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.

# Partition to K Equal Sum Subsets

> Tested Python solution for LeetCode 698 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 698, Medium. Topics: Array, Dynamic Programming, Backtracking, Bit Manipulation, Memoization, Bitmask. [View on LeetCode](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/).

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

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

## Problem

Given an integer array `nums` and an integer `k`, return `true` if it is possible to divide this array into `k` non-empty subsets whose sums are all equal.

### Examples

```
Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
```

```
Input: nums = [1,2,3,4], k = 3
Output: false
```

### Constraints

* 1 \<= k \<= nums.length \<= 16
* 1 \<= nums\[i] \<= 10^4
* The frequency of each element is in the range \[1, 4].

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(k * 2^n)
    # Space: O(n)
    def can_partition_k_subsets(self, nums: list[int], k: int) -> bool:
        total = sum(nums)
        if total % k != 0:
            return False

        target = total // k
        nums.sort(reverse=True)
        if nums[0] > target:
            return False

        buckets = [0] * k

        def backtrack(index: int) -> bool:
            if index == len(nums):
                return True
            for bucket_index in range(k):
                if buckets[bucket_index] + nums[index] <= target:
                    buckets[bucket_index] += nums[index]
                    if backtrack(index + 1):
                        return True
                    buckets[bucket_index] -= nums[index]
                # Prune: empty bucket means placement here is symmetric to any
                # other empty bucket; also a just-filled bucket that failed.
                if buckets[bucket_index] == 0:
                    break
            return False

        return backtrack(0)
```

## Complexity

| Time        | Space |
| ----------- | ----- |
| O(k \* 2^n) | O(n)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
