Advertisement
i_love_rao_khushboo

Untitled

Sep 10th, 2022
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. bool is_subset(vi &v, int sum, int n) {
  2.     // initialisation of dp matrix by false
  3.     for(int i = 0; i <= sum; i++) dp[i] = false;
  4.     dp[0] = true;
  5.    
  6.     // choice diagram code iterative version
  7.     for(int i = 0; i < n; i++) {
  8.         // the element to be included in the sum cannot be greater than the sum
  9.         for(int j = sum; j >= v[i]; j--) {
  10.             // check if sum - v[i] could be formed from a subset using elements before index i
  11.             if(dp[j - v[i]]) dp[j] = 1;
  12.         }
  13.     }
  14.    
  15.     return dp[sum];
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement