698. Partition to K Equal Sum Subsets
Partition to K Equal Sum Subsets (LeetCode 698) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Backtracking Patterns pattern, in the Combination Sum subpattern.
How to solve Partition to K Equal Sum Subsets
Partition to K Equal Sum Subsets is a Combination Sum problem, part of Backtracking Patterns. Find combinations that sum to target using backtracking. Handle reuse and duplicates.
Work towards the solution in these steps:
- Compute the total sum first. The total must be divisible by `k`, and the quotient is the required sum of every group.
- Construct one group at a time by choosing unused values whose running sum does not exceed the target.
- Once a group's running sum reaches the target, begin constructing the next group. The search succeeds after all values have been assigned.
The reference solution runs in O(n * 2^n) time and O(2^n) space.
The Partition to K Equal Sum Subsets solution and editorial walks through the full approach with reference code.
Problem statement
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.
Every group must have the same sum. Return true if such a partition is possible; otherwise, return false. The order of the groups and the order of elements within a group are irrelevant.
Example 1:
Input: nums = [6,10,14,18], k = 2 Output: true Explanation: The total is 48, so each group must sum to 24. The elements can be split into {6, 18} and {10, 14}.
Example 2:
Input: nums = [2,2,2,2,2,2,2,2,9], k = 5 Output: false Explanation: The total is 25, so each group would need sum 5, but the value 9 alone exceeds that target.
Example 3:
Input: nums = [3,5,6,7,9,10], k = 2 Output: true Explanation: Each group must sum to 20. One valid partition is {3, 7, 10} and {5, 6, 9}.
Constraints:
- 4 <= nums.length <= 16 - 1 <= nums[i] <= 10^4 - 1 <= k <= nums.length - The sum of all values in nums is divisible by k - The sum of all values in nums is at most 10^4
Topics and companies
Topics: Array, Backtracking, Depth-First Search, Memoization.
How to practise Partition to K Equal Sum Subsets on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n * 2^n) time and O(2^n) space.
Open Partition to K Equal Sum Subsets in the code editor, or read the Partition to K Equal Sum Subsets editorial for a worked solution with its approach and complexity analysis.
Where Partition to K Equal Sum Subsets sits in the DSA pattern sheet
Problems related to Partition to K Equal Sum Subsets
Other problems that use the same Combination Sum and Backtracking Patterns techniques:
- Combination Sum — Medium, same subpattern
- Combination Sum II — Medium, same subpattern
- Check if Word Can Be Placed In Crossword — Medium, same pattern
- Combinations — Medium, same pattern
- Generate Parentheses — Medium, same pattern
- Letter Combinations of a Phone Number — Medium, same pattern
Partition to K Equal Sum Subsets FAQ
What is the LeetCode number of Partition to K Equal Sum Subsets?
Partition to K Equal Sum Subsets is LeetCode problem 698, rated Medium.
What pattern is used to solve Partition to K Equal Sum Subsets?
Partition to K Equal Sum Subsets is a Combination Sum problem. It belongs to the Backtracking Patterns pattern, in the Combination Sum subpattern.
What is the time complexity of Partition to K Equal Sum Subsets?
The reference solution runs in O(n * 2^n) time and O(2^n) space.