-
Notifications
You must be signed in to change notification settings - Fork 18
/
EqualSumPartition.cpp
75 lines (56 loc) · 1.44 KB
/
EqualSumPartition.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
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
bool subsetSum(vector<int> &v, int n, int sum, vector<vector<int>> &memo)
{
// Base condition
// We can return empty set
if (sum == 0)
return true;
if (n == 0)
return false;
// Check memo once before making recursive calls
if (memo[n][sum] != -1)
return memo[n][sum];
// Store result in memo instead of returning it directly
if (v[n - 1] <= sum)
memo[n][sum] = subsetSum(v, n - 1, sum - v[n - 1], memo) or
subsetSum(v, n - 1, sum, memo);
else
memo[n][sum] = subsetSum(v, n - 1, sum, memo);
return memo[n][sum]; // And then return memo
}
bool equal_sum_partition(vector<int> &v, int n)
{
// Get the sum of the array
int sum = accumulate(v.begin(), v.end(), 0);
// If sum is odd
if (sum & 1)
return false;
// If sum is even
sum = sum / 2;
// Apply subset sum as subroutine
vector<vector<int>> memo(
n + 1, vector<int>(sum + 1, -1)); // Initialize all values with -1
if (subsetSum(v, n, sum, memo))
return true;
return false;
}
int main()
{
int n, sum;
cout << "Enter the size of the array: ";
cin >> n;
vector<int> v(n);
cout << "Enter Elements of array: ";
for (int i = 0; i < n; i++)
cin >> v[i];
cout << "Enter Sum: ";
cin >> sum;
if (equal_sum_partition(v, n))
cout << "Equal sum partition found\n";
else
cout << "No such partition found\n";
return 0;
}