CodeMentor.
Ask AIProblemsLearnC++TCSCore CSHRProgress
0Lv 1
ProblemsArrays

Maximum Subarray (Kadane)

Medium
LeetCode
7/14
MediumArraysLeetCode #53

Kadane's Algorithm

At each index: extend the previous subarray or start fresh?

cur = max(x, cur+x), ans = max(ans, cur).

Examples

Example 1

Input

9
-2 1 -3 4 -1 2 1 -5 4

Output

6
Example 2

Input

1
1

Output

1

Stuck? Take a nudge

C++17Autosaves