#698

Partition to K Equal Sum Subsets

Medium Backtracking with pruning

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether the array can be split into k groups that all have the same sum.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can the numbers be split into k groups with equal sums?

Why this works: each group must hit total / k. Place the largest numbers first into buckets and backtrack on failure; skip buckets that look the same.

Example: [4, 3, 2, 3, 5, 2, 1], k = 4 → target 5

5 | 4+1 | 3+2 | 3+2 ✓

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not sorting in descending order — big numbers placed last cause much more backtracking.

Solve on LeetCode ↗
Complexity
  • Time: O(k^n) worst case, far less with pruning.
  • Space: O(n).
Topic: Recursion & Backtracking
🎬 Episodes 29–36: video coming soon