forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEqual_sum_partition.cpp
123 lines (94 loc) · 2.75 KB
/
Equal_sum_partition.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
/*
Problem description:
Given a set of positive numbers, find if we can equally divide
the set into two subsets such that the sum of elements in both the
subsets is equal.
APPROACH
---------------------------------------------------------------
We will use Dynamic Programming approach to solve this problem.
Firstly, we will check whether the given array is of even length
or not, if not the answer will be always NO, else we will check
whether a subset lies in the array such that its sum is equal to
half the sum of all the elements of the array, if such subset
exists then answer would be YES, else answer is NO.
*/
#include<bits/stdc++.h>
using namespace std;
bool EqualPartition(int* array, int array_size ){
int array_sum=0;
//Finding the sum of all elements of array
for(int i=0;i<array_size;i++){
array_sum = array_sum + array[i];
}
// Checking whether array sum is odd or not, if odd, return false
if((array_sum % 2) != 0){
return false;
}
// Dividing array sum by 2
array_sum = array_sum / 2;
bool dp[array_size + 1][array_sum + 1]; //Declaring dp matrix
//Initialising DP matrix with true and false
for(int i=0; i<=array_size; i++){
for(int j=0; j<=array_sum; j++){
if(i==0)
dp[i][j]=false;
if(j==0)
dp[i][j]=true;
}
}
/*
Filling DP matrix int dp[][], where dp[i][j] stores whether a given sum = j,
can be obtained as a sum of elements of a subset, using the first (i+1) elements
of array.
*/
for(int i=1; i<=array_size; i++){
for(int j=1; j<=array_sum; j++){
if(dp[i][j] <= array_sum){
dp[i][j] = ( dp[i][j-array[i-1]] ) || (dp[i-1][j]);
}
else{
dp[i][j] = dp[i-1][j];
}
}
}
/*
Return the last element of the DP matrix, which stores true, if sum = (sum of array/2)
can be obtained as a sum of elements of a subset, using all the elements of array, else
it stores false.
*/
return dp[array_size][array_sum];
}
int main(){
int array_len;
cin >> array_len;
int my_array[array_len + 1];
for(int i=0; i<array_len; i++){
cin >> my_array[i];
}
//Function call to check whether the array can be divided into two subsets of equal sum or not.
if(EqualPartition(my_array, array_len)){
cout<<"YES"<<"\n";
}
else{
cout<<"NO"<<"\n";
}
return 0;
}
/*
Sample Test Cases:
Input:
6
2 3 4 2 3 2
Output:
YES
Input:
5
4 2 5 3 1
Output:
NO
Time Complexity: O(sum*size)
Auxiliary Space: O(sum*size)
where,
sum = sum of array/2
size= size of array
*/