-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subset Sum Problem.cpp
159 lines (116 loc) · 3.04 KB
/
Subset Sum Problem.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
PROBLEM:
Given a set of numbers, check whether it can be partitioned into two subsets such that the sum of elements in both subsets is same or not.
Input:
The first line contains an integer 'T' denoting the total number of test cases. Each test case constitutes of two lines. First line contains
'N', representing the number of elements in the set and the second line contains the elements of the set.
Output:
Print YES if the given set can be partioned into two subsets such that the sum of elements in both subsets is equal, else print NO.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= arr[i] <= 1000
Example:
Input:
2
4
1 5 11 5
3
1 3 5
Output:
YES
NO
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,i,s=0,j;
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
s+=a[i];
bool ans=false;
if(s%2==0)
{
int m=s/2;
bool dp[n+1][m+1]; //dp[i][j] represents that sum j can be made or not
//using elements upto index i
dp[0][0]=true;
for(j=1;j<=m;j++)
dp[0][j]=false; //without using any element we cannot make sum
//greater than 0
for(i=1;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(a[i-1]>j) //1 based indexing
{
dp[i][j]=dp[i-1][j];
}
else
{
int need=j-a[i-1];
if(dp[i-1][j]==true || dp[i-1][need]==true)
dp[i][j]=true;
else
dp[i][j]=false;
}
}
}
ans=dp[n][m];
}
if(ans==true)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
O(sum) space complexity:
class Solution {
public:
bool canPartition(vector<int>& nums) {
int i,j,n,s=0;
n=nums.size();
for(i=0;i<n;i++)
s+=nums[i];
if(s%2==1)
return false;
vector<bool> dp(s+1,false);
dp[0]=true;
for(i=0;i<n;i++)
{
for(j=s;j>=nums[i];j--)
{
if(dp[j-nums[i]])
dp[j]=true;
}
}
return dp[s/2];
}
};
time complexity O(n) and space complexity O(max sum/4) solution using bitwise:
class Solution {
public:
bool canPartition(vector<int>& nums) {
int i,j,n,s=0;
n=nums.size();
for(i=0;i<n;i++)
s+=nums[i];
if(s%2==1)
return false;
bitset<20001> b(1);
for(i=0;i<n;i++)
{
b = b | (b<<nums[i]);
if(b[s/2])
return true;
}
return b[s/2];
}
};