<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 {
 public:
 int solve(vector<int>&nums,int index,int k,unordered_map<int,int>&mp){
    if(index>=nums.size())return 1;
    int notTake=solve(nums,index+1,k,mp);
    int take=0;
     if(!mp[nums[index] - k] && !mp[nums[index] + k]){
        mp[nums[index]]++;
        take=solve(nums,index+1,k,mp);
        mp[nums[index]]--;
       
    }
    return take+notTake;
 }   
 public:
 int solve(int i,vector<int>&a,int n,int k,int prev,vector<vector<int>>& dp){
    if(i>=n)return 1;
    if(dp[i][prev]!=-1)return dp[i][prev];
    int notTake=solve(i+1,a,n,k,prev,dp);
    int take=0;
    if(prev==0||abs(a[i]-prev)!=k){
        take=solve(i+1,a,n,k,a[i],dp);
    }
    return dp[i][prev]= take+notTake;
 }
public:
    int beautifulSubsets(vector<int>& nums, int k) {
        int n=nums.size();
     sort(nums.begin(),nums.end());
     vector<vector<int>> dp(k);
     for(auto it:nums){
        dp[it%k].push_back(it);
     }
     int ans=1;
     for(int i=0;i<k;i++){
        if(dp[i].size()>0){
            int m=dp[i].size();
            vector<vector<int>> dp1(m,vector<int>(dp[i][m-1]+1,-1));
            ans=ans*solve(0,dp[i],m,k,0,dp1);
        }
     }
     return ans-1;
    }
};
63782