-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtarget_sum.cpp
50 lines (38 loc) · 1.17 KB
/
target_sum.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
45
46
47
48
49
50
class Solution {
public:
int countSubsetsWithSumDP(vector<int> v, int sum) {
int n = v.size();
int dp[n + 1][sum + 1];
for(int i=0; i<=sum; i++) {
dp[0][i] = false;
}
for(int j=0; j<=n; j++) {
dp[j][0] = true;
}
for(int i=1; i<=n; i++) {
for(int j=0; j<=sum; j++) {
if(v[i-1] > j) {
dp[i][j] = dp[i-1][j];
}
else {
//include + exclude
dp[i][j] = dp[i-1][j] + dp[i-1][j - v[i-1]];
}
}
}
return dp[n][sum];
}
int countSubsetWithDifferenceK(vector<int> v, int difference) {
long long arr_sum = 0, sum = 0;
for(auto n : v) arr_sum += n;
long long int _t = arr_sum + difference;
if((_t) % 2 != 0) return 0;
if(difference > arr_sum) return 0;
else
sum = (difference + arr_sum) / 2;
return countSubsetsWithSumDP(v, sum);
}
int findTargetSumWays(vector<int>& nums, int S) {
return countSubsetWithDifferenceK(nums, S);
}
};