Python | Backtracking
see the Successfully Accepted Submission\n# Code\n\nclass Solution:\n    def beautifulSubsets(self, nums: List[int], k: int) -> int:\n        def backtrack(star
[see the Successfully Accepted Submission](\nhttps://leetcode.com/problems/the-number-of-beautiful-subsets/?envType=daily-question&envId=2024-05-23)\n# Code\n```\nclass Solution:\n    def beautifulSubsets(self, nums: List[int], k: int) -> int:\n        def backtrack(start, current_subset):\n            nonlocal count\n            \n            # Only count non-empty subsets\n            if current_subset:\n                count += 1\n            \n            for i in range(start, len(nums)):\n                # Check if current element nums[i] can be added to the current_subset\n                can_add = True\n                for num in current_subset:\n                    if abs(num - nums[i]) == k:\n                        can_add = False\n                        break\n                        \n                if can_add:\n                    # Include nums[i] in the subset and proceed\n                    current_subset.append(nums[i])\n                    backtrack(i + 1, current_subset)\n                    # Backtrack: remove the last added element\n                    current_subset.pop()\n        \n        nums.sort()  # Sorting to make the checking more efficient\n        count = 0\n        backtrack(0, [])\n        return count\n        \n```\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']