Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool is_subset(vi &v, int sum, int n) {
- // initialisation of dp matrix by false
- for(int i = 0; i <= sum; i++) dp[i] = false;
- dp[0] = true;
- // choice diagram code iterative version
- for(int i = 0; i < n; i++) {
- // the element to be included in the sum cannot be greater than the sum
- for(int j = sum; j >= v[i]; j--) {
- // check if sum - v[i] could be formed from a subset using elements before index i
- if(dp[j - v[i]]) dp[j] = 1;
- }
- }
- return dp[sum];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement