Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Combination Sum
Brute Force
Backtracking + Pruning
1
combinationSum([2,3,6,7], 7)
Example 1
Multiple Solutions
No Solution
Reuse Elements
Many Candidates
Custom
candidates
=
[2, 3, 6, 7]
,
target
=
7
cand
2
0
3
1
6
2
7
3
target 7
result
0/2
cand
2
0
3
1
6
2
7
3
target 7
result
0/2
[2,2,2,2]
-1
[]
7
[2]
5
[2,2]
3
[2,2,2]
1
cand
2
0
3
1
6
2
7
3
start
2
2
2
target 7
remaining
1
result
0/2
[2,2,2,2]
-1
[2,2,2,3]
-2
[2,2,2,6]
-5
[2,2,2,7]
-6
[]
7
[2]
5
[2,2]
3
[2,2,2]
1
cand
2
0
3
1
6
2
7
3
start
2
2
2
target 7
remaining
1
result
0/2
[2,2,2]
1
[2,2,2,2]
-1
[2,2,2,3]
-2
[2,2,2,6]
-5
[2,2,2,7]
-6
[2,2,6]
-3
[]
7
[2]
5
[2,2]
3
[2,2,3]
[2,2,7]
-4
cand
2
0
3
1
6
2
7
3
start
2
2
7
target 7
remaining
-4
result
1/2
[2,2,3]
[2,2]
3
[2,2,2]
1
[2,2,2,2]
-1
[2,2,2,3]
-2
[2,2,2,6]
-5
[2,2,2,7]
-6
[2,2,6]
-3
[2,2,7]
-4
[2,3,3]
-1
[]
7
[2]
5
[2,3]
2
[2,2,3]
[2,3,6]
-4
cand
2
0
3
1
6
2
7
3
start
2
3
6
target 7
remaining
-4
result
1/2
[2,2,3]
[2,2]
3
[2,2,2]
1
[2,2,2,2]
-1
[2,2,2,3]
-2
[2,2,2,6]
-5
[2,2,2,7]
-6
[2,2,6]
-3
[2,2,7]
-4
[2,3]
2
[2,3,3]
-1
[2,3,6]
-4
[2,3,7]
-5
[2,6]
-1
[2,7]
-2
[]
7
[2]
5
[2,2,3]
cand
2
0
3
1
6
2
7
3
start
2
7
target 7
remaining
-2
result
1/2
[2,2,3]
[2]
5
[2,2]
3
[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3,7]
-3
[6]
1
[6,6]
-5
[6,7]
-6
[2,2,3]
[7]
cand
2
0
3
1
6
2
7
3
target 7
result
2/2
[2,2,3]
[7]
Step:
Start: Find combinations that sum to target
0 / 113
Example 1
Multiple Solutions
No Solution
Reuse Elements
Many Candidates
Custom
candidates
=
[2, 3, 6, 7]
,
target
=
7
0 / 113
Step:
Start: Find combinations that sum to target
1
combinationSum([2,3,6,7], 7)