Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Maximum Subarray
Bookmark
Input
Classic
Mostly Positive
All Negative
Single Element
Custom
nums
=
[-2, 1, -3, 4, -1, 2, 1]
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
current =
1
best =
-2
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
-2 < 0
→ start fresh: current =
1
current =
1
best =
1
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
1
>
best
-2
→ best =
1
current =
-2
best =
1
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
-2
≤ best
1
→ best stays
current =
4
best =
1
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
-2 < 0
→ start fresh: current =
4
current =
3
best =
4
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current =
4
+
(-1)
=
3
current =
3
best =
4
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
3
≤ best
4
→ best stays
current =
5
best =
5
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current
5
>
best
4
→ best =
5
current =
6
best =
5
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
i
current =
5
+
1
=
6
current =
6
best =
6
0
1
2
3
4
5
6
-2
1
-3
4
-1
2
1
Max subarray sum =
6
algo
master
.
io
Step:
Start of algorithm
0 / 14
Input
Classic
Mostly Positive
All Negative
Single Element
Custom
nums
=
[-2, 1, -3, 4, -1, 2, 1]
0 / 14
algo
master
.
io
Step:
Start of algorithm