Here's a C++ solution for dividing set A into k subsets where each subset's sum equals the corresponding value in set K:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class SetPartitioner {
private:
vector<int> A;
vector<int> K;
vector<vector<int>> result;
vector<bool> used;
bool backtrack(int idx, vector<int>& currentSums, vector<vector<int>>& currentSets) {
// If we've assigned all elements from A
if (idx == A.size()) {
// Check if all sums match K values
for (int i = 0; i < K.size(); i++) {
if (currentSums[i] != K[i]) {
return false;
}
}
result = currentSets;
return true;
}
// Try placing current element in each subset
for (int i = 0; i < K.size(); i++) {
// Pruning: skip if adding current element exceeds target
if (currentSums[i] + A[idx] > K[i]) {
continue;
}
// Add element to subset i
currentSums[i] += A[idx];
currentSets[i].push_back(A[idx]);
// Recursively try to place remaining elements
if (backtrack(idx + 1, currentSums, currentSets)) {
return true;
}
// Backtrack
currentSums[i] -= A[idx];
currentSets[i].pop_back();
}
return false;
}
public:
bool partition(vector<int>& setA, vector<int>& setK) {
A = setA;
K = setK;
// Check if sum of A equals sum of K
int sumA = accumulate(A.begin(), A.end(), 0);
int sumK = accumulate(K.begin(), K.end(), 0);
if (sumA != sumK) {
return false;
}
// Sort A in descending order for better pruning
sort(A.begin(), A.end(), greater<int>());
// Initialize tracking arrays
vector<int> currentSums(K.size(), 0);
vector<vector<int>> currentSets(K.size());
return backtrack(0, currentSums, currentSets);
}
vector<vector<int>> getResult() {
return result;
}
};
int main() {
// Example usage
vector<int> A = {4, 3, 2, 3, 5, 2, 1};
vector<int> K = {5, 7, 8};
SetPartitioner partitioner;
cout << "Set A: ";
for (int val : A) cout << val << " ";
cout << "\n";
cout << "Target sums K: ";
for (int val : K) cout << val << " ";
cout << "\n\n";
if (partitioner.partition(A, K)) {
cout << "Solution found:\n";
vector<vector<int>> result = partitioner.getResult();
for (int i = 0; i < K.size(); i++) {
cout << "Subset " << i + 1 << " (sum = " << K[i] << "): ";
for (int val : result[i]) {
cout << val << " ";
}
cout << "\n";
}
} else {
cout << "No solution exists\n";
}
return 0;
}
This solution uses a backtracking approach to find a valid partition. Here's how it works:
-
Input Validation: First checks if the sum of all elements in A equals the sum of all target values in K. If not, no valid partition exists.
-
Backtracking Algorithm:
- Tries to place each element of A into one of the k subsets
- Maintains running sums for each subset
- Prunes branches where adding an element would exceed the target sum
- Backtracks when a path doesn't lead to a solution
-
Optimization: Sorts array A in descending order to fail faster on impossible branches.
-
Time Complexity: O(k^n) in the worst case, where n is the size of set A and k is the number of subsets.
The example in main() demonstrates partitioning A = {4, 3, 2, 3, 5, 2, 1} into 3 subsets with sums {5, 7, 8}.
You can modify the input arrays A and K to test different scenarios. The algorithm will return whether a valid partition exists and display the subsets if found.