Partition to K Equal Sum Subsets

Given positive integers values and an integer k, decide whether all elements can be partitioned into exactly k nonempty groups with the same sum. Every…

Given positive integers values and an integer k, decide whether all elements can be partitioned into exactly k nonempty groups with the same sum. Every indexed element must belong to one group. Groups need not be contiguous, labeled, ordered, or equal in size. Equal-valued elements at different indices are separate items. Return a native Boolean and leave the input array unchanged.

- 1 <= values.length <= 16. - Every value is an integer with 1 <= values[i] <= 10000. - k is an integer with 1 <= k <= values.length. - Boolean, fractional, string-coerced, zero and negative entries are invalid. - Return true or false, not 0/1, a string, a count or a grouping witness.

Example 1: input values = [4,3,2,3,5,2,1], k = 4; output true. Groups [5], [4,1], [3,2] and [3,2] all sum to 5.

Example 2: input values = [1,1,1,3], k = 2; output true. Groups [3] and [1,1,1] have different sizes but equal sums.

Example 3: input values = [1,4,6,7], k = 2; output false. The total 18 is divisible by 2, but no subset sums to the required 9.

Category: Recursion. Topic: recursion. Difficulty: Medium. Patterns: backtracking, bit-manipulation, dynamic-programming.