Partition of a set into K subsets with equal sum using BitMask and DP
Given an integer array arr[] of consisting of N integers, the task is check if it is possible to divide the given array into K non-empty subsets of equal sum such that every array element is part of a single subset.
Examples:
Input: arr[] = {2, 1, 4, 5, 6}, K = 3
Output: Yes
Explanation:
Possible subsets of given array are {2, 4}, (1, 5} and {6}Input: arr[] = {2, 1, 5, 5, 6}, K = 3
Output: No
For the recursive approach, refer to Partition of a set into K subsets with equal sum.
Approach:
Follow the steps below to solve the problem:
- The idea is to use mask to determine the current state. The current state will tell us about the subset already formed ( which numbers are already selected ).
For example: arr[] = [2, 1, 4, 3, 5, 6, 2], mask = (1100101), which means that { 2, 1, 5, 2 } are already chosen in the current mask. - For any current state mask, the jth element will be added to it based on the following two conditions:
- The jth bit is not set in the mask (mask&(1<<j) == 0)
- sum (mask) + arr[j] <= target ( where target = (Sum of array elements) / K)
- Maintian a table dp[] such that dp[i] store the sum of elements in mask i. So, the dp transitions will be:
dp[i | (1 << j)] = (dp[i] + arr[j]) % target
Illustration:
arr [ ] = [4, 3, 2, 3, 5, 2, 1], K = 4, tar = 5
dp["1100101"] implies that { 4, 3, 5, 1 } are chosen
Hence, Sum = 4 + 3 + 5 + 1 = 13, 13 % 5 = 3.
Hence, dp["1100101"] = 3If dp["11111…1111"] == 0 then that means we can find the solution.
Below is the implementation of above approach:
C++
// C++ program to check if the // given array can be partitioned // into K subsets with equal sum #include <bits/stdc++.h> using namespace std; // Function to check whether // K required partitions // are possible or not bool isKPartitionPossible(int arr[], int N, int K) { if (K == 1) // Return true as the // entire array is the // answer return true; // If total number of // partitions exceeds // size of the array if (N < K) return false; int sum = 0; for (int i = 0; i < N; i++) sum += arr[i]; // If the array sum is not // divisible by K if (sum % K != 0) // No such partitions are // possible return false; // Required sum of // each subset int target = sum / K; // Initialize dp array with -1 int dp[(1 << 15)]; for (int i = 0; i < (1 << N); i++) dp[i] = -1; // Sum of empty subset // is zero dp[0] = 0; // Iterate over all subsets/masks for (int mask = 0; mask < (1 << N); mask++) { // if current mask is invalid, continue if (dp[mask] == -1) continue; // Iterate over all array elements for (int i = 0; i < N; i++) { // Check if the current element // can be added to the current // subset/mask if (!(mask & (1 << i)) && dp[mask] + arr[i] <= target) { // transition dp[mask | (1 << i)] = (dp[mask] + arr[i]) % target; } } } if (dp[(1 << N) - 1] == 0) return true; else return false; } // Driver Code int main() { int arr[] = { 2, 1, 4, 5, 3, 3 }; int N = sizeof(arr) / sizeof(arr[0]); int K = 3; if (isKPartitionPossible(arr, N, K)) { cout << "Partitions into equal "; cout << "sum is possible.\n"; } else { cout << "Partitions into equal "; cout << "sum is not possible.\n"; } } |
Time Complexity: O (N * 2 N)
Auxiliary Space: O (2 N)
Got what you were looking for? Learn more and become self sufficient. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Paced course, and that too at the most student-friendly price.
Recommended Posts:
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Closest sum partition (into two subsets) of numbers from 1 to n
- Count number of ways to partition a set into k subsets
- Partition a set into two subsets such that the difference of subset sums is minimum
- Check if it possible to partition in k subarrays with equal sum
- Partition the array into three equal sum segments
- Equal sum array partition excluding a given element
- Print equal sum sets of array (Partition Problem) | Set 2
- Find Partition Line such that sum of values on left and right is equal
- Print equal sum sets of array (Partition problem) | Set 1
- Count of subsets with sum equal to X
- Count ways to partition a string such that both parts have equal distinct characters
- Count of subsets with sum equal to X using Recursion
- Divide first N natural numbers into 3 equal sum subsets
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

