forked from Vishal-Aggarwal0305/DSA-CODE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumSubsets.cpp
More file actions
40 lines (32 loc) · 773 Bytes
/
minimumSubsets.cpp
File metadata and controls
40 lines (32 loc) · 773 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int>arr{1, 1, 2, 3};
// cout<<arr[2];
int n = arr.size(), sum = 0, diff = 1;
for(auto x: arr){
sum+=x;
}
sum = (diff+sum)/2;
int dp[n+1][sum+1];
for(int i = 0; i< sum+1; i++){
dp[0][i] = 0;
}
for(int i = 0; i< n+1; i++){
dp[i][0] = 1;
}
for(int i=1; i< n+1; i++){
for(int j = 1; j<sum+1; j++){
// means we are considering previous element
if(arr[i-1]<=j){
// for current elementb
dp[i][j] = dp[i-1][j] + dp[i-1][j-arr[i-1]];
}
else{
dp[i][j] = dp[i-1][j];
}
}
}
cout<<dp[n][sum];
return 0;
}