Partition to K Equal Sum Subsets
Given an integer array nums and an integer k, determine whether it is possible to divide the array into k non-empty subsets whose sums are all equal.
Open official problem prompt ↗Decide whether all elements can be partitioned into k groups that each add up to total/k.
Like dealing cards into k piles so every pile has the same point total; you fill one pile to the goal, then move on to the next, backtracking whenever a pile cannot be completed.
- Input
- nums = [4,3,2,3,5,2,1], k = 4
- Output
- true
- Why
- The total is 20, so each subset must sum to 5: (5), (1,4), (2,3), and (2,3) are four equal-sum subsets using every element.
1 <= k <= nums.length <= 161 <= nums[i] <= 10^4The sum of all nums does not exceed 2^31 - 1