-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathThe Number of Beautiful Subsets.cpp
44 lines (30 loc) · 1.07 KB
/
The Number of Beautiful Subsets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Solution by Rahul Surana
***********************************************************
You are given an array nums of positive integers and a positive integer k.
A subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.
Return the number of non-empty beautiful subsets of the array nums.
A subset of nums is an array that can be obtained by deleting some (possibly none) elements from nums.
Two subsets are different if and only if the chosen indices to delete are different.
***********************************************************
*/
class Solution {
public:
unordered_map<int,int> a;
int df(int i,vector<int>& nums, int &k){
if(i==nums.size()) {
return 1;
}
int t = 0;
if(!a[nums[i]-k] && !a[nums[i]+k]){
a[nums[i]]++;
t = df(i+1,nums,k);
a[nums[i]]--;
}
int nt = df(i+1,nums,k);
return nt+t;
}
int beautifulSubsets(vector<int>& nums, int &k) {
return df(0,nums,k)-1;
}
};