Combination Sum III
Backtracking
Asked at 4 companies
2 recent
- Amazon
- Microsoft
- Bloomberg
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Building a new string or array as the answer · Replacing recursion with your own stack · Recursion and the call stack · The base case: where the calls stop · Depth-first traversal: preorder, inorder, postorder · Binary numbers and AND, OR, XOR
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Sudoku solvers and regex: Try a choice, go deeper, and undo it when it fails. Solvers fill puzzles this way, and many regex engines match patterns the same way.
About this problem
Choose, explore, un-choose, with pruning when the sum or count goes too far.
Try it in the editor below for 25–35 min: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- k = 3n = 9
- Output
- [[1, 2, 6], [1, 3, 5], [2, 3, 4]]
Walk through it step by step(29 steps · shows the approach, so try first)
- Choose 3 different digits 1–9 that add up to 9. Try digits in increasing order; stop a branch when the sum goes over or we have 3 already.
- Try 1: chosen [1], 8 left.
- Try 2: chosen [1, 2], 6 left.
- 1 + 2 + 6 = 9: a .
- Try 3: chosen [1, 3], 5 left.
- 1 + 3 + 5 = 9: a combination.
- Try 4: chosen [1, 4], 4 left.
- Try 5: chosen [1, 5], 3 left.
- Try 6: chosen [1, 6], 2 left.
- Try 7: chosen [1, 7], 1 left.
- Try 8: chosen [1, 8], 0 left.
- Try 2: chosen [2], 7 left.
- Try 3: chosen [2, 3], 4 left.
- 2 + 3 + 4 = 9: a combination.
- Try 4: chosen [2, 4], 3 left.
- Try 5: chosen [2, 5], 2 left.
- Try 6: chosen [2, 6], 1 left.
- Try 7: chosen [2, 7], 0 left.
- Try 3: chosen [3], 6 left.
- Try 4: chosen [3, 4], 2 left.
- Try 5: chosen [3, 5], 1 left.
- Try 6: chosen [3, 6], 0 left.
- Try 4: chosen [4], 5 left.
- Try 5: chosen [4, 5], 0 left.
- Try 5: chosen [5], 4 left.
- Try 6: chosen [6], 3 left.
- Try 7: chosen [7], 2 left.
- Try 8: chosen [8], 1 left.
- Try 9: chosen [9], 0 left.
Example 2
No valid answer- Input
- k = 4n = 1
- Output
- []
Example 3
- Input
- k = 3n = 7
- Output
- [[1, 2, 4]]
- The answer can be in any order.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionover the digits 1–9 in increasing order, each used at most once. Stop a branch as soon as the sum overshoots n or k numbers are already chosen.
class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
result, path = [], []
def explore(start, remaining):
if len(path) == k:
if remaining == 0:
result.append(path[:])
return
for d in range(start, 10):
if d > remaining:
break
path.append(d)
explore(d + 1, remaining - d)
path.pop()
explore(1, n)
return resultReference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.