15% off all plans & roadmaps Use code
See plansProblem editorial
Given nums, an array of positive integers, and k, determine whether all elements can be divided into exactly k nonempty groups. Each element must be assigned to one group, and no element may be used more than once.
Partition to K Equal Sum Subsets is a Backtracking Patterns problem, in the Combination Sum subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
The reference solution runs in O(n * 2^n) time and O(2^n) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Partition to K Equal Sum Subsets is a Backtracking Patterns problem, in the Combination Sum subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.