Back to problems Solve on LeetCode →

Maximum Subarray

LeetCode 53 • Medium • Arrays

Find the contiguous subarray with the largest sum. Return the sum. Kadane: track current_sum and max_sum; reset current_sum when it goes negative.

TimeO(n)single pass
SpaceO(1)two variables
current_sum: max_sum:
Input
Current subarray
Max subarray
current_sum
max_sum
Ready
Press Play. Kadane's algorithm: for each element, either extend current_sum or reset to the element. Update max_sum when current exceeds it.