Kadane's Algorithm

2-minute video explanation

Intuition

You scan numbers from left to right and keep a running sum of a current subarray:

“Keep the current sum while it’s helpful (non-negative).
The moment it turns harmful (negative), throw it away and start over.”
While doing this, you keep track of the best sum you’ve ever seen.

Code example

current_max = nums[0]   # sum of subarray ending here
global_max = nums[0]    # best sum seen so far

for x in nums[1:]:
    # If adding x makes it worse than just x alone, start new at x
    current_max = max(x, current_max + x)

    # Track the best we’ve ever seen
    global_max = max(global_max, current_max)

So:

Time and Space Complexity