1815. Equal Sum Groupings Count
A teacher wants to split a class of n students into exactly k non-empty teams, where nums[i] is the score of student i, so that all teams have the same total score. Students are told apart by their index, even when scores are equal, and the teams are unordered: two splits are the same when they consist of the same teams.
Return the number of different ways to partition the indices 0..n-1 into k groups with equal sums, as a long; return 0 if there is none. A natural way to count each split once is to build one group at a time, always starting the next group with the smallest unused index and adding further indices in increasing order. The partial sum of the current group follows from the set of used indices, so a memo keyed by the used set and the last added index brings the cost to O(2^n * n^2) time and O(2^n * n) space.
Example 1
- Input:
- nums = [2,2,2,2], k = 2
- Output:
- 3
- Explanation:
Four indices split into two pairs in 3 ways, and every pair sums to 4.
Example 2
- Input:
- nums = [1,2,3,4,5,5], k = 2
- Output:
- 3
- Explanation:
Each group must sum to 10: either the two 5s against 1+2+3+4, or one 5 with 4+1 and the other with 3+2 (two choices of which 5), giving 3 splits.
Example 3
- Input:
- nums = [1,2], k = 2
- Output:
- 0
- Explanation:
The groups would be [1] and [2] with sums 1 and 2, which differ, so the answer is 0.
Constraints
1 ≤ k ≤ n ≤ 14
1 ≤ nums[i] ≤ 104
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms
Expected complexity
- Time
- O(2^n * n^2)
- Space
- O(2^n * n)