Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Target Sum
Bookmark
Recursion
Memoization
Subset Sum DP
Input
Example 1
Example 2
Example 3
Example 4
Custom
nums
=
[1, 1, 1, 1, 1]
,
target
=
3
nums (index, currentSum)
1
1
1
1
1
nums (index, currentSum)
1
1
1
1
1
nums (index, currentSum)
1
1
1
1
1
0,0
1,1
2,2
3,3
4,4
1
5,5
0
5,3
1
4,2
5,3
1
5,1
3,1
4,2
5,3
5,1
4,0
5,1
5,-1
2,0
3,1
4,2
5,3
5,1
4,0
5,1
5,-1
3,-1
4,0
5,1
5,-1
4,-2
5,-1
5,-3
1,-1
2,0
3,1
4,2
5,3
5,1
4,0
5,1
3 hits the target
nums (index, currentSum)
1
1
1
1
1
0,0
1,1
2,2
3,3
2
4,4
1
5,5
0
5,3
1
4,2
1
5,3
1
5,1
0
3,1
4,2
1
5,3
1
5,1
0
4,0
5,1
0
5,-1
2,0
3,1
4,2
5,3
5,1
4,0
5,1
5,-1
3,-1
4,0
5,1
5,-1
4,-2
5,-1
5,-3
1,-1
2,0
3,1
4,2
5,3
5,1
4,0
5,1
1 misses the target
nums (index, currentSum)
1
1
1
1
1
0,0
1,1
2,2
3
3,3
2
4,4
1
5,5
0
5,3
1
4,2
1
5,3
1
5,1
0
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5,-1
0
2,0
3,1
4,2
1
5,3
1
5,1
0
4,0
5,1
5,-1
3,-1
4,0
5,1
5,-1
4,-2
5,-1
5,-3
1,-1
2,0
3,1
4,2
5,3
5,1
4,0
5,1
backtrack(4, 0)
nums (index, currentSum)
1
1
1
1
1
0,0
1,1
2,2
3
3,3
2
4,4
1
5,5
0
5,3
1
4,2
1
5,3
1
5,1
0
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5,-1
0
2,0
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5,-1
0
3,-1
4,0
0
5,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1
0
4,0
0
5,1
0
5,-1
0
3,-1
0
4,0
0
5,1
0
5,-1
0
4,-2
0
5,-1
0
5,-3
0
1,-1
2,0
1
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
backtrack(4, -2)
nums (index, currentSum)
1
1
1
1
1
0,0
5
1,1
4
2,2
3
3,3
2
4,4
1
5,5
0
5,3
1
4,2
1
5,3
1
5,1
0
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5,-1
0
2,0
1
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5,-1
0
3,-1
0
4,0
0
5,1
0
5,-1
0
4,-2
0
5,-1
0
5,-3
0
1,-1
1
2,0
1
3,1
1
4,2
1
5,3
1
5,1
0
4,0
0
5,1
0
5 way(s) reach 3
algo
master
.
io
Step:
5 numbers and a target of 3. Give each number a + or a -.
0 / 189
Input
Example 1
Example 2
Example 3
Example 4
Custom
nums
=
[1, 1, 1, 1, 1]
,
target
=
3
0 / 189
algo
master
.
io
Step:
5 numbers and a target of 3. Give each number a + or a -.