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

# The Number of Beautiful Subsets

> Tested Python solution for LeetCode 2597 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2597, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming), [Backtracking](/catalog/topics/backtracking), [Sorting](/catalog/topics/sorting), Combinatorics. [View on LeetCode](https://leetcode.com/problems/the-number-of-beautiful-subsets/description/).

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

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

## Problem

You are given an array `nums` of positive integers and a **positive** integer `k`.

A subset of `nums` is **beautiful** if it does not contain two integers with an absolute difference equal to `k`.

Return *the number of **non-empty beautiful** subsets of the array* `nums`.

A **subset** of `nums` is an array that can be obtained by deleting some (possibly none) elements from `nums`. Two subsets are different if and only if the chosen indices to delete are different.

### Examples

```
Input: nums = [2,4,6], k = 2
Output: 4
```

**Explanation:** The beautiful subsets of the array nums are: `[2]`, `[4]`, `[6]`, `[2, 6]`. It can be proved that there are only 4 beautiful subsets in the array `[2,4,6]`.

```
Input: nums = [1], k = 1
Output: 1
```

**Explanation:** The beautiful subset of the array nums is `[1]`. It can be proved that there is only 1 beautiful subset in the array `[1]`.

### Constraints

* `1 <= nums.length <= 18`
* `1 <= nums[i], k <= 1000`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n + n)
    # Space: O(n)
    def beautiful_subsets(self, nums: list[int], k: int) -> int:
        freq: dict[int, int] = {}
        for num in nums:
            freq[num] = freq.get(num, 0) + 1

        total = 1
        for residue in {num % k for num in nums}:
            vals = sorted(v for v in freq if v % k == residue)
            # f = valid subsets over values seen so far (empty included);
            # f_prev2 = same, excluding the most recent value.
            f, f_prev2, prev = 1, 0, None
            for val in vals:
                if prev is not None and val - prev == k:
                    # Cannot pick both val and prev.
                    f, f_prev2 = ((1 << freq[val]) - 1) * f_prev2 + f, f
                else:
                    f, f_prev2 = (1 << freq[val]) * f, f
                prev = val
            total *= f
        return total - 1
```

## Complexity

| Time | Space |
| - | - |
| O(n log n + n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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