Backtracking with Unordered map simple solution || CPP code
\n# Approach\n Describe your approach to solving the problem. \nFor each number of the nums array, we check is nums[i]-k or nums[i]+k is already present in the 
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor each number of the nums array, we check is `nums[i]-k` or `nums[i]+k` is already present in the subarray. This checking can be done by maintaining an unordered_map. If such a number is already present in the subarray then we dont explore that branch of the recursion. If not present then we simply add the current number to the map and recursively call for the next element of the `nums` array.\n\n# Complexity\n- Time complexity:$$O(2^n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nWhere n is the number of elements of the `nums` array.\n# Code\n```\nclass Solution {\npublic:\n    void dfs(vector<int>& nums, int k, unordered_map<int, int>& freq, int& ans, int index) {\n        if (index == nums.size()) {\n            ans++;\n            return;\n        }\n\n        // Exclude the current number\n        dfs(nums, k, freq, ans, index + 1);\n\n        // Include the current number, if valid\n        if (freq[nums[index] - k] == 0 && freq[nums[index] + k] == 0) {\n            freq[nums[index]]++;\n            dfs(nums, k, freq, ans, index + 1);\n            freq[nums[index]]--;\n        }\n    }\n\n    int beautifulSubsets(vector<int>& nums, int k) {\n        unordered_map<int, int> freq;\n        int ans = 0;\n        dfs(nums, k, freq, ans, 0);\n        return ans - 1;  //excluding the empty subset\n    }\n};\n\n```
['Hash Table', 'Backtracking', 'Depth-First Search', 'Recursion', 'C++']