-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqual_Sum_Partition.cpp
More file actions
52 lines (48 loc) · 799 Bytes
/
Equal_Sum_Partition.cpp
File metadata and controls
52 lines (48 loc) · 799 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
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
#define M 1001 //Assuming the size of input
using namespace std;
int t[M][M];
bool Subset_Sum(vector<int> &arr, int sum, int n)
{
//Initialization
for(int i=0; i<M; i++)
{
t[0][i] = 0;
t[i][0] = 1;
}
//Choice diagram
for(int i=1; i<n+1; i++)
{
for(int j=1; j<sum+1; j++)
{
if(j>=arr[i-1])
t[i][j] = t[i-1][j]||t[i-1][j-arr[i-1]];
else
t[i][j] = t[i-1][j];
}
}
return t[n][sum];
}
bool Equal_Sum_Partition(vector<int> &arr, int n)
{
int sum = 0;
for(int i=0; i<n; i++)
sum += arr[i];
if(sum%2!=0)
return 0;
else
return Subset_Sum(arr,sum/2,n);
}
int main()
{
int n;
cin>>n;
vector<int> arr(n);
for(int i=0; i<n; i++)
cin>>arr[i];
if(Equal_Sum_Partition(arr,n))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}