Skip to main content

Partition to K Equal Sum Subsets - Solution & Explanation

MediumArrayDynamic ProgrammingBacktrackingBit Manipulation38 min readAsked at: Amazon, Microsoft, Meta +5
Practice this problem

Problem Statement

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 <= 16
  • 1 <= nums[i] <= 104
  • The frequency of each element is in the range [1, 4].

Approach Overview

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< is reachable, all elements were partitioned successfully.

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.

Approach 1: Backtracking with Target Subset Sum

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Dynamic Programming and State Compression

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.

Code

C

C++

Java

Complexity

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.

Try this approach in the editor →

Approach 3: DFS + Pruning

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 4: State Compression + Memoization

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:

  • If 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.
  • Otherwise, add the 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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 5: Dynamic Programming

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Backtracking with Target Subset Sum

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.

Dynamic Programming and State Compression

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.

DFS + Pruning
State Compression + Memoization
Dynamic Programming

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Backtracking with Target Subset SumO(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.

Video Solution

Partition To K Equal Sum Subsets From An Array of Integers - The Backtracking ApproachBack To Back SWE84,552 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Partition to K Equal Sum Subsets easy or hard?
Partition to K Equal Sum Subsets is considered Medium on LeetCode but often feels closer to Hard for beginners. The difficulty comes from pruning the exponential search space and recognizing patterns like bucket filling or bitmask DP.
Partition to K Equal Sum Subsets Python or Java solution?
Python and Java implementations usually follow the same pattern: compute the target subset sum, sort the array descending, and use recursive backtracking to place numbers into k buckets. Bitmask DP versions are also common in Java and C++ because bit operations are efficient.
How to solve Partition to K Equal Sum Subsets in O(n * 2^n)?
Use dynamic programming with bitmask state compression. Each mask represents which elements are already used. The DP state stores the current subset sum modulo the target value. Transition by adding unused elements while keeping the partial sum ≤ target. Reaching the full mask means all numbers were partitioned successfully.
What is the best approach for Partition to K Equal Sum Subsets?
Backtracking with target subset sums is the most common approach. Sort the array in descending order and recursively assign numbers to k buckets while keeping each bucket's sum ≤ target. With pruning and symmetry checks, the search space reduces significantly. Time complexity is roughly O(k * 2^n) with O(n) recursion space.
Is Partition to K Equal Sum Subsets asked at Google Amazon Meta?
Partition to K Equal Sum Subsets appears frequently in interviews at companies that test recursion and combinatorial search, including Google, Amazon, and Meta-style interview prep sets. The problem evaluates backtracking optimization, pruning strategies, and understanding of subset state representations.
What data structure is used in Partition to K Equal Sum Subsets?
The typical solution uses arrays to track bucket sums and recursion for backtracking. Advanced implementations also use bitmasks and hash-based memoization to store visited subset states, enabling dynamic programming with compressed state representation.
What is the time complexity of Partition to K Equal Sum Subsets?
The optimized backtracking solution runs in about O(k * 2^n) time because it explores subset combinations while pruning invalid states. The dynamic programming solution with bitmask state compression runs in O(n * 2^n) time and uses O(2^n) memory for DP states.

Ready to solve this problem?

Practice Partition to K Equal Sum Subsets with our built-in code editor and test cases.

Practice on FleetCode