Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Target Sum
Bookmark
Input
Example 1
Example 2
Example 3
Example 4
Custom
nums
=
[1, 1, 1, 1, 1]
,
target
=
3
nums
±
1
±
1
±
1
±
1
±
1
target =
3
put + or - before every num, how many ways to reach 3?
nums
±
1
±
1
±
1
±
1
±
1
target =
3
put + or - before every num, how many ways to reach 3?
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
dp
1
0
0
1
0
2
0
3
0
4
num = 1
walk j from
4
down to
1
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
dp
1
0
1
1
0
2
0
3
0
4
num =
1
folded in, dp[4] =
0
ways so far
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
+1
dp
1
0
1
1
1
2
0
3
0
4
j-1 = 1
j = 2
dp[2]
=
0
+
1
=
1
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
+1
dp
1
0
2
1
1
2
0
3
0
4
j-1 = 2
j = 3
dp[3]
+= dp[3 -
1
] = dp[
2
] =
1
way
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
+1
dp
1
0
3
1
3
2
1
3
0
4
j-1 = 0
j = 1
dp[1]
=
2
+
1
=
3
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
+1
dp
1
0
3
1
3
2
4
3
1
4
j-1 = 2
j = 3
dp[3]
=
1
+
3
=
4
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
dp
1
0
4
1
6
2
4
3
1
4
num =
1
folded in, dp[4] =
1
ways so far
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
num
+1
dp
1
0
4
1
6
2
10
3
5
4
j-1 = 1
j = 2
dp[2]
+= dp[2 -
1
] = dp[
1
] =
4
ways
nums
1
1
1
1
1
target =
3
sum =
5
S+ =
4
dp
1
0
5
1
10
2
10
3
5
4
dp[4] = 5
, 5 ways to hit target
3
algo
master
.
io
Step:
Assign + or - to every num so the expression evaluates to 3. Count the ways.
0 / 49
Input
Example 1
Example 2
Example 3
Example 4
Custom
nums
=
[1, 1, 1, 1, 1]
,
target
=
3
0 / 49
algo
master
.
io
Step:
Assign + or - to every num so the expression evaluates to 3. Count the ways.