Skip to main content

Partition to K Equal Sum Subsets - Video Solutions

MediumArrayDynamic ProgrammingBacktrackingBit ManipulationMemoization

Partition To K Equal Sum Subsets From An Array of Integers - The Backtracking Approach

Back To Back SWE
11:0384,552 views
10 video solutions available

Partition to K Equal Sum Subsets - Video Solution

Watch 10 video solutions for Partition to K Equal Sum Subsets, a medium level problem involving Array, Dynamic Programming, Backtracking. This walkthrough by Back To Back SWE has 84,552 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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].
Read full problem with examples

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.

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.