Skip to content

Partition to K Equal Sum Subsets

MediumBacktracking
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of positive integers nums and an integer k, decide whether you can split nums into k non-empty groups with equal sums. Every number must go into exactly one group.

nums = [4, 3, 2, 3, 5, 2, 1], k = 4  →  True    ([5], [1, 4], [2, 3], [2, 3])
nums = [1, 2, 3, 4],          k = 3  →  False

Constraints

  • 1 <= k <= nums.length <= 16
  • 1 <= nums[i] <= 10^4