Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // check whether the given equal sum partition is possible or not.
- // here we dont have wt array
- // s1 +s2 = total sum
- // as s1 = s2 ====> check whether total sum/2 exists or not.
- int solve(vector<int> value){
- int n = value.size();
- int sum=0;
- for(int a=0;a<n;a++)sum+=value[a];
- if(sum%2 == 1) return 0; // as odd sum can't be partitioned into two .
- vector<vector<bool>> dp (n+1 ,vector<bool>(sum+1,false));
- // a represents the ath element .. b represents the knapsack present
- for(int a=0;a<=n;a++)dp[a][0] = true;// sum 0 is possible even if do not select anything
- for(int a=1;a<=n;a++){
- for(int b=1;b<=sum;b++){
- if(value[a-1]<=b){
- dp[a][b] = dp[a-1][b-value[a-1]] || dp[a-1][b]; //either i choose the ath element or not
- }else{
- dp[a][b] = dp[a-1][b]; // cant choose the ath element becuase its weight is more
- }
- }
- }
- return dp[n][sum/2];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement