-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubset-Sum-Equal-to-K.cpp
35 lines (32 loc) · 1.16 KB
/
Subset-Sum-Equal-to-K.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
#include <bits/stdc++.h>
using namespace std;
// Return true if there is a subset of arr[] with sum equal to given sum
bool solve(vector<int> &arr, int idx, int n, int k, vector<vector<int>> &dp) {
// If left sum is 0, then there is a subset
if(k == 0) {
return true;
}
// If there are no elements and left sum is negative,
// then there is no subset
if(k < 0 || idx >= n) {
return false;
}
// If we have already solved this subproblem, return
// the result from the dp table
if(dp[k][idx] != -1) {
return dp[k][idx];
}
// Take the current element into the subset and check
// if there is a subset with the remaining sum
bool take = solve(arr, idx + 1, n, k - arr[idx], dp);
// Don't take the current element into the subset and check
// if there is a subset with the remaining sum
bool notTake = solve(arr, idx + 1, n, k, dp);
// Store the result in the dp table
return dp[k][idx] = (take || notTake);
}
bool subsetSumToK(int n, int k, vector<int> &arr) {
vector<vector<int>> dp(k + 1, vector<int>(n, -1));
// Call the recursive function
return solve(arr, 0, n, k, dp);
}