Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Combinations
Brute Force
Backtracking + Pruning
1
combine(4, 2)
Example 1: n=4, k=2
Larger: n=5, k=3
Small: n=3, k=1
Three of Four: n=4, k=3
Custom
n
=
4
,
k
=
2
+1
+2
+3
+4
+2
+3
+4
+3
+4
+4
nums
1
2
3
4
current
result
0/6
+1
+2
+3
+4
+2
+3
+4
+3
+4
+4
nums
1
2
3
4
current
result
0/6
+2
+3
+4
+2
+3
+4
+3
+4
+4
+1
[]
[1]
nums
1
2
3
4
start
current
1
result
0/6
+3
+4
+2
+3
+4
+3
+4
+4
+2
+1
[]
[1,2]
[1]
nums
1
2
3
4
start
current
1
3
result
1/6
[1,2]
+2
+3
+4
+3
+4
+4
+2
+3
+1
+4
[]
[1]
[1,2]
[1,3]
[1,4]
nums
1
2
3
4
start
current
1
4
result
2/6
[1,2]
[1,3]
+2
+3
+4
+3
+4
+4
+1
+2
+3
+4
[1]
[1,2]
[1,3]
[1,4]
[]
nums
1
2
3
4
start
current
2
result
3/6
[1,2]
[1,3]
[1,4]
+4
+3
+4
+4
+1
+2
+3
+4
+3
+2
[1]
[]
[1,2]
[1,3]
[1,4]
[2,3]
[2]
nums
1
2
3
4
start
current
2
3
result
4/6
[1,2]
[1,3]
[1,4]
[2,3]
+3
+4
+4
+1
+2
+3
+4
+3
+4
+2
[1]
[]
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[2]
nums
1
2
3
4
start
current
2
result
5/6
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
+4
+1
+2
+2
+3
+4
+3
+4
+3
+4
[1]
[2]
[]
[3]
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
nums
1
2
3
4
start
current
3
4
result
5/6
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
+4
+1
+2
+3
+2
+3
+4
+3
+4
+4
[1]
[2]
[3]
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
[]
nums
1
2
3
4
start
current
4
result
6/6
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
+1
+2
+3
+4
+2
+3
+4
+3
+4
+4
[]
[1]
[2]
[3]
[4]
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
nums
1
2
3
4
current
result
6/6
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
Step:
Start: find all combinations of k numbers from 1 to n
0 / 45
Example 1: n=4, k=2
Larger: n=5, k=3
Small: n=3, k=1
Three of Four: n=4, k=3
Custom
n
=
4
,
k
=
2
0 / 45
Step:
Start: find all combinations of k numbers from 1 to n
1
combine(4, 2)