VOOZH about

URL: https://www.geeksforgeeks.org/dsa/partition-of-a-set-into-k-subsets-with-equal-sum-using-bitmask-and-dp/

⇱ Partition into k Equal Sum Subsets - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Partition into k Equal Sum Subsets

Last Updated : 14 Apr, 2026

Given an integer array arr[] and an integer k, the task is to 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: true
Explanation: Possible subsets of the given array are [2, 4], [1, 5] and [6]

Input: arr[] = [2, 1, 5, 5, 6], k = 3 
Output: false
Explanation: It is not possible to divide above array into 3 parts with equal sum.

Using Recursion - O(k*2^n) Time and O(n*k) Space

We recursively explore all possible combinations for each of the k subsets. This is achieved by tracking the sum of the current subset and using a boolean array (taken) to check if an element has already been included in a subset or not.

Base Cases:

  • if k = 1, the entire array forms the only subset.
  • if n < k, it is impossible to divide the array into k subset since there are not enough elements.
  • if the total sum of array is not divisible by k, equal partitioning is not feasible.

if these condition are met, the task reduces to dividing the array into k subsets , each with sum equal to arraySum/k.

Recursive Cases:

The recursive function attempts to add elements to each subset:

  • If a subset’s sum matches the required value, the function moves to the next subset.
  • If a subset doesn’t reach the target, the function backtracks and tries other combinations of elements.
  • Once k-1 subsets reach the required sum, the remaining elements automatically form the final subset with the desired sum, confirming that partitioning is possible.

Steps

  • Calculate total sum and check base conditions (k == 1, n < k, sum % k != 0)
  • Set target = sum / k and initialize taken[] and subsetSum[]
  • Use recursion to try adding unused elements to the current subset while keeping sum ≤ target
  • If a subset reaches target, move to the next subset; if k-1 subsets are formed, return true
  • Apply backtracking (undo choices) if no valid combination works, and finally return false if partition is not possible.

Output
true


Using Bitmasking and DP - O(n*2^n) and O(2^n) Space

The idea is to use mask to determine the current state. The current state tells 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 statemask, 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)

Maintain 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 

  • Calculate sum, check divisibility by k, set target
  • Use dp[mask] to store current subset sum, initialize dp[0] = 0
  • For each mask, try adding unused element j if sum ≤ target
  • Update: dp[newMask] = (dp[mask] + arr[j]) % target
  • Continue till all masks are processed
  • If final mask is valid → return true, else false

Output
true
Comment
Article Tags: