-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubset_sum.cpp
More file actions
61 lines (50 loc) · 1.07 KB
/
subset_sum.cpp
File metadata and controls
61 lines (50 loc) · 1.07 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// https://practice.geeksforgeeks.org/problems/subset-sum-problem/0
#include<bits/stdc++.h>
using namespace std;
int n, ar[105], dp[100005][105];
bool solve(int sum)
{
memset(dp, false, sizeof(dp));
int c_sum, c_ind;
for(c_ind = 0 ; c_ind < n ; c_ind++)
dp[0][c_ind] = true;
for(c_sum = 1 ; c_sum <= sum ; c_sum++)
{
for(c_ind = 0 ; c_ind < n ; c_ind++)
{
if(c_sum >= ar[c_ind]) // check if current element >= current element
dp[c_sum][c_ind] = dp[c_sum - ar[c_ind]][c_ind - 1] or dp[c_sum][c_ind - 1]; // either consider the element or leave out the element
else
dp[c_sum][c_ind] = dp[c_sum][c_ind - 1]; // not consider the element
}
}
return dp[sum][n-1];
}
int main()
{
int t, i, sum;
cin>>t;
while (t--)
{
memset(dp, 0, sizeof(dp)); // reset dp table
sum = 0;
cin>>n;
for(i = 0 ; i < n ; i++)
{
cin>>ar[i];
sum += ar[i];
}
if(sum % 2 == 1) // if total sum is odd, partition is not possible
{
printf("NO\n");
continue;
}
else
sum /= 2;
if(solve(sum))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}