Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4,3,2,3,5,2,1], k = 4 Output: true Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Example 2:
Input: nums = [1,2,3,4], k = 3 Output: false
Constraints:
1 <= k <= nums.length <= 161 <= nums[i] <= 104[1, 4].Problem Overview: You get an integer array nums and an integer k. The task is to determine whether the array can be split into exactly k subsets where every subset has the same total sum. Each element must belong to exactly one subset.
The first observation: if the total array sum is not divisible by k, the answer is immediately false. If it is divisible, each subset must sum to target = totalSum / k. The challenge becomes assigning numbers to k buckets so that every bucket reaches this target without reusing elements.
Approach 1: Backtracking with Target Subset Sum (Time: O(k * 2^n), Space: O(n))
This approach builds subsets recursively using backtracking. Sort the array in descending order, then try to place each number into one of the k buckets. Maintain an array representing the current sum of every bucket. For each number, iterate through the buckets and attempt to place it if the new sum does not exceed the target.
The key insight is aggressive pruning. If placing a number into an empty bucket fails, there is no reason to try other empty buckets because they are symmetric. Sorting the array largest-first also reduces branching since larger values constrain placements earlier. This drastically cuts the search space and works well for the constraint n ≤ 16. The algorithm effectively explores subset combinations while ensuring each bucket never exceeds the target.
Approach 2: Dynamic Programming with State Compression (Time: O(n * 2^n), Space: O(2^n))
This approach uses dynamic programming with bitmasking to represent which elements are already used. A bitmask of size n indicates selected elements, and the DP value stores the current subset sum modulo the target. Iterate through all masks and try to add another unused element.
If adding nums[i] keeps the subset sum ≤ target, transition to the new mask. When the partial sum reaches the target, reset it to zero to start building the next subset. If the final mask (1<
The state compression technique avoids explicit bucket tracking and instead encodes subset membership in bits. This produces a deterministic O(n * 2^n) solution and works well for smaller arrays where bitmask DP is feasible.
Recommended for interviews: Start with the Backtracking with Target Subset Sum approach. It demonstrates strong problem decomposition, pruning strategies, and recursive reasoning. Interviewers commonly expect this solution. The bitmask DP version shows deeper knowledge of 2^n state compression and is a strong follow-up optimization when discussing advanced techniques.
This approach involves using backtracking to attempt to build k subsets, each with a sum equal to totalSum / k. The decision tree can be pruned by sorting the numbers in decreasing order and tracking the sums of the current subset.
This solution begins by calculating the total sum of elements and checks early if this is divisible by k. It uses sorting to try larger numbers first, which helps in quicker pruning of impossible branches. The recursive function tries to add each number to the current subset sum and backtracks when necessary.
Time Complexity: O(kn), where n is the number of elements, due to exploring all partition possibilities.
Space Complexity: O(n + k) due to recursive call stack and subset sums array.
A less intuitive but potentially powerful technique involves using bitmasking to represent subsets and top-down dynamic programming to evaluate the feasibility of partitioning. This uses state compression where each state represents a set of elements already partitioned; 1 means used, 0 means unused.
This approach encodes subsets using bitmasking where the nth bit represents the nth element in the nums array. Dynamic programming is used to store the remainder when each mask's subset sum is divided by targetSubsetSum. A successful partition is found when the full mask covers all elements with a remainder of 0.
Time Complexity: O(n * 2n), determined by the states and transitions across the full bitmask.
Space Complexity: O(2n), which is used to store DP states for all subset configurations.
According to the problem description, we need to partition the array nums into k subsets such that the sum of each subset is equal. Therefore, we first sum all the elements in nums. If the total sum cannot be divided by k, it means we cannot partition the array into k subsets, and we return false early.
If the total sum can be divided by k, let's denote the expected sum of each subset as s. Then, we create an array cur of length k to represent the current sum of each subset.
We sort the array nums in descending order (to reduce the number of searches), and then start from the first element, trying to add it to each subset in cur one by one. If adding nums[i] to a subset cur[j] makes the subset's sum exceed s, it means it cannot be placed in that subset, and we can skip it. Additionally, if cur[j] is equal to cur[j - 1], it means we have already completed the search for cur[j - 1], and we can skip the current search.
If we can add all elements to cur, it means we can partition the array into k subsets, and we return true.
Python
Java
C++
Go
TypeScript
Similar to Solution 1, we first check whether the array nums can be partitioned into k subsets. If it cannot be divided by k, we directly return false.
Let s be the expected sum of each subset, and let state represent the current partitioning state of the elements. For the i-th number, if the i-th bit of state is 0, it means the i-th element has not been partitioned.
Our goal is to form k subsets with a sum of s from all elements. Let t be the current sum of the subset. When the i-th element is not partitioned:
t + nums[i] \gt s, it means the i-th element cannot be added to the current subset. Since we sort the array nums in ascending order, all elements from position i onwards cannot be added to the current subset, and we directly return false.i-th element to the current subset, change the state to state | 2^i, and continue searching for unpartitioned elements. Note that if t + nums[i] = s, it means we can form a subset with a sum of s. The next step is to reset t to zero (which can be achieved by (t + nums[i]) bmod s) and continue partitioning the next subset.To avoid repeated searches, we use an array f of length 2^n to record the search results for each state. The array f has three possible values:
0 indicates that the current state has not been searched yet;-1: indicates that the current state cannot be partitioned into k subsets;1: indicates that the current state can be partitioned into k subsets.The time complexity is O(n times 2^n), and the space complexity is O(2^n). Here, n represents the length of the array nums. For each state, we need to traverse the array nums, which has a time complexity of O(n). The total number of states is 2^n, so the overall time complexity is O(n times 2^n).
Python
Java
C++
Go
TypeScript
We can use dynamic programming to solve this problem.
We define f[i] to represent whether there exists k subsets that meet the requirements when the current selected numbers' state is i. Initially, f[0] = true, and the answer is f[2^n - 1], where n is the length of the array nums. Additionally, we define cur[i] to represent the sum of the last subset when the current selected numbers' state is i.
We enumerate the states i in the range [0, 2^n]. For each state i, if f[i] is false, we skip it. Otherwise, we enumerate any number nums[j] in the array nums. If cur[i] + nums[j] > s, we break the enumeration loop because the subsequent numbers are larger and cannot be placed in the current subset. Otherwise, if the j-th bit of the binary representation of i is 0, it means the current nums[j] has not been selected. We can place it in the current subset, change the state to i | 2^j, update cur[i | 2^j] = (cur[i] + nums[j]) bmod s, and set f[i | 2^j] = true.
Finally, we return f[2^n - 1].
The time complexity is O(n times 2^n), and the space complexity is O(2^n). Here, n represents the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Backtracking with Target Subset Sum | Time Complexity: O(kn), where n is the number of elements, due to exploring all partition possibilities. |
| Dynamic Programming and State Compression | Time Complexity: O(n * 2n), determined by the states and transitions across the full bitmask. |
| DFS + Pruning | — |
| State Compression + Memoization | — |
| Dynamic Programming | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Target Subset Sum | O(k * 2^n) | O(n) | Best general solution. Works well with pruning and small n (≤16). |
| Dynamic Programming with Bitmask (State Compression) | O(n * 2^n) | O(2^n) | Useful when representing subsets with bitmasks or discussing advanced DP optimizations. |
Partition To K Equal Sum Subsets From An Array of Integers - The Backtracking Approach • Back To Back SWE • 84,024 views views
Watch 9 more video solutions →Practice Partition to K Equal Sum Subsets with our built-in code editor and test cases.
Practice on FleetCode