-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKPartitionProblem.cpp
More file actions
106 lines (87 loc) · 1.94 KB
/
KPartitionProblem.cpp
File metadata and controls
106 lines (87 loc) · 1.94 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
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
#include <iostream>
#include <vector>
#include <numeric>
// QUESTION: https://www.techiedelight.com/k-partition-problem-print-all-subsets/
class KPartition
{
private:
const std::vector<int>& arr;
std::vector<std::vector<int>> subsetsFound;
public:
KPartition(const std::vector<int>& a)
: arr(a)
{
}
void find(int k)
{
int sum = std::accumulate(arr.begin(), arr.end(), 0);
if ((sum % k) != 0) {
std::cout << "The array cannot be partitioned into " << k << " subsets of equal sum" << std::endl;
} else {
std::cout << "The array can be partitioned into " << k << " subsets of equal sum, and they are:" << std::endl;
std::vector<int> sumArray = std::vector<int>(k, sum / k);
subsetsFound.resize(k);
kPartitionHelper(arr.size() - 1, sumArray, k);
for (int i = 0; i < subsetsFound.size(); ++i)
{
std::cout << "Partition " << i << " : ";
for (int j : subsetsFound[i])
{
std::cout << j << " ";
}
std::cout << std::endl;
}
}
}
private:
bool check(std::vector<int>& sumArray)
{
bool result = true;
for (int j : sumArray)
{
if (j != 0)
{
result = false;
break;
}
}
return result;
}
bool kPartitionHelper(int curr_indx,
std::vector<int>& sumArray,
int k)
{
// Solution found
if (check(sumArray))
{
return true;
}
// Cannot continue
if (curr_indx < 0)
return false;
bool included = false;
for (int i = 0; i < k; ++i)
{
if (!included && (sumArray[i] - arr[curr_indx] >= 0))
{
subsetsFound[i].push_back(arr[curr_indx]);
sumArray[i] = sumArray[i] - arr[curr_indx];
included = kPartitionHelper(curr_indx - 1, sumArray, k);
if (!included)
{
subsetsFound[i].pop_back();
sumArray[i] = sumArray[i] + arr[curr_indx];
}
}
}
return included;
}
};
int main()
{
const std::vector<int> arr{7, 3, 5, 12, 2, 1, 5, 3, 8, 4, 6, 4};
int k = 5;
KPartition obj(arr);
obj.find(k);
return 0;
}