Search RU
Tasks / Algorithms / Maximum subarray sum
Statement medium

Maximum subarray sum

Implement `max_subarray(nums)`: the maximum sum of a non-empty contiguous
subarray.

Rules:

- an empty list returns `0`;
- if every number is negative, the answer is the largest (least negative) number,
not `0`;
- overflow is not a concern: Python integers are unbounded.

Example: `[-2, 1, -3, 4, -1, 2, 1, -5, 4]` → `6`, the subarray `[4, -1, 2, 1]`.

The expected solution is one pass (Kadane's algorithm): at each step decide
whether to extend the current run or start a new one at the current element.

Python
algorithmsarrayskadane
solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

What do you need to know about the best run that ends at the current element, so
that you do not enumerate every subarray?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts