<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>

<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>

<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,6], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> 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].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
It can be proved that there is only 1 beautiful subset in the array [1].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>1 &lt;= nums[i], k &lt;= 1000</code></li>
</ul>

class Solution:
    def beautifulSubsets(self, nums: List[int], k: int) -> int:
        groups = []
        counts = collections.defaultdict(int)
        cache = dict()

        def backtrack(num, freq):
            if num not in freq:
                return 1
            
            if num in cache:
                return cache[num]
            
            not_take = backtrack(num+k, freq)
            take = (2**(freq[num])-1) * backtrack(num+2*k, freq)
            cache[num] = take+not_take

            return cache[num]
        
        
        for n in nums:
            counts[n] += 1
        visited = set()
        for key in counts:
            if key in visited:
                continue
            
            group = {}
            while key-k in counts:
                key -= k
            while key in counts:
                visited.add(key)
                group[key]  = counts[key]
                key += k
            
            groups.append(group)
        # print("ggg", groups)
        res = 1
        for g in groups:
            n = min(g.keys())
            res *= backtrack(n, g)

        return res-1

            

        # def backtrack(idx, curr_set):
        #     if idx >= len(nums):
        #         return 0
        #     res = 0
        #     for i in range(idx, len(nums)):
        #         if not (curr_set[(nums[i] - k)] or curr_set[(nums[i]+k)]):
        #             curr_set[(nums[i])] += 1
        #             res += 1 + backtrack(i+1, curr_set)
        #             # if nums[i] in curr_set:
        #             curr_set[(nums[i])] -= 1

        #     return res  

        # return backtrack(0, collections.defaultdict(int))
16900