Python || Backtracking with Counter
Approach\nSince you should check all the subsets, it\'s acceptable to Backtrack all the possible permutations. But how to check, whether nums[j] - nums[i] != k?
# Approach\nSince you should check all the subsets, it\'s acceptable to Backtrack all the possible permutations. But how to check, whether `nums[j] - nums[i] != k`? Initialize `count` array with the max possible size from `max(nums, k) + 1`. Do the regular BT and check, if you have already seen a result of subtraction.\n\n# Complexity\n- Time complexity: **O(2^N) + O(N log N)**\n\n- Space complexity: **O(N)**\n\n# Code\n```python\nclass Solution:\n    def beautifulSubsets(self, nums: list[int], k: int) -> int:\n        ans = 0\n        nums.sort()\n        n = len(nums)\n        count = [0] * (max(max(nums), k) + 1)\n\n        def bt(i, path):\n            if i <= n:\n                if path:\n                    nonlocal ans\n                    ans += 1\n\n            for j in range(i, n):\n                diff = nums[j] - k\n\n                if count[diff] <= 0:\n                    count[nums[j]] += 1\n                    bt(j + 1, path + [nums[j]])\n                    count[nums[j]] -= 1\n\n        bt(0, [])\n\n        return ans\n```
['Backtracking', 'Sorting', 'Python3']