Combination Sum II
Backtracking
Asked at 12 companies
4 recent
- Microsoft
- Bloomberg
- Amazon
- Rakuten
- Snap
- Morgan Stanley
- Adobe
- Walmart Labs
- Zoho
+2 moreShow fewer
- Meta
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
Try a choice, recurse, undo it — subsets, permutations, and search.
Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- candidates = [1, 1, 2, 5, 6]target = 8
- Output
- [[1, 1, 6], [1, 2, 5], [2, 6]]
Walk through it step by step(18 steps · shows the approach, so try first)
- Each number can be used once. Sorted input lets us skip a value that equals the previous one at the same level (avoids duplicate answers) and stop once numbers get too big.
- Take 1: path [1], total 1.
- Take 1: path [1, 1], total 2.
- Take 2: path [1, 1, 2], total 4.
- Take 5: path [1, 1, 5], total 7.
- Take 6: path [1, 1, 6], total 8.
- 1 + 1 + 6 = 8 — record it.
- Take 2: path [1, 2], total 3.
- Take 5: path [1, 2, 5], total 8.
- 1 + 2 + 5 = 8 — record it.
- Take 5: path [1, 5], total 6.
- Take 6: path [1, 6], total 7.
- Take 2: path [2], total 2.
- Take 5: path [2, 5], total 7.
- Take 6: path [2, 6], total 8.
- 2 + 6 = 8 — record it.
- Take 5: path [5], total 5.
- Take 6: path [6], total 6.
Example 2
Just one itemNo valid answer- Input
- candidates = [3]target = 2
- Output
- []
Example 3
Duplicates- Input
- candidates = [2, 5, 2, 1, 2]target = 5
- Output
- [[1, 2, 2], [5]]
- 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 solutionSort, then where each number can be used once (recurse with i + 1). Skip a candidate equal to the previous one at the same level to avoid duplicate , and stop once a candidate exceeds what's left.
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
result, path = [], []
def build(start: int, remaining: int) -> None:
if remaining == 0:
result.append(path[:])
return
for i in range(start, len(candidates)):
if i > start and candidates[i] == candidates[i - 1]:
continue
if candidates[i] > remaining:
break
path.append(candidates[i])
build(i + 1, remaining - candidates[i])
path.pop()
build(0, target)
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.