Max subarray of [-2, 1, -3, 4, -1, 2, 1, -5, 4] vs [-1, -2, -3] overview diagram
Why
Why Brute Force Fails on Maximum Subarray Problems
Imagine you are handed the sequence nums=[−2,1,−3,4,−1,2,1,−5,4] and asked to find the contiguous subarray that yields the largest possible sum. If you approach this with a naive brute-force mindset, you might think of checking every single starting and ending index combination. With N elements, there are 2N(N+1) possible subarrays, which explodes quickly as N grows. For our specific array of 9 elements, that means evaluating 45 distinct slices. If your array scales to 100,000 elements, brute force requires billions of operations, dragging performance down to a crawl. Worse yet, what happens when you encounter an array like nums=[−1,−2,−3] where every number is negative? A naive approach that defaults to an empty sum of 0 will completely fail the constraint that at least one element must be selected. Kadane's maximum subarray explained starts right here: how do we inspect every relevant contiguous slice in a single pass without recomputing sums from scratch?
Why Brute Force Fails on Maximum Subarray Problems diagram
Model
Building the Mental Model for Kadane's Algorithm
When examining our running target array nums=[−2,1,−3,4,−1,2,1,−5,4], a naive O(n2) slice check forces us to recompute overlapping windows repeatedly. Kadane's algorithm replaces this redundancy with a dynamic programming mindset, asking a single local question at each index i: do I extend the previous running subarray, or do I drop it and start a fresh subarray right here?
Let cur be the maximum sum of a subarray ending at the current position, and best be the global maximum found so far across the entire sequence. As we step through elements from left to right, cur updates via max(nums[i],cur+nums[i]). If the accumulated cur ever drops below the standalone value of nums[i], it means dragging along the old history hurts more than it helps—so we jettison the past and restart our subarray at nums[i]. This exact mechanism also protects us when handling our second test case, nums=[−1,−2,−3], ensuring we pick the least damaging single element rather than mistakenly settling on a default sum of 0.
Building the Mental Model for Kadane's Algorithm diagram
Worked example
Tracing Kadane's Algorithm Step by Step on [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Let us execute our model on the primary array from our locked example: nums=[−2,1,−3,4,−1,2,1,−5,4]. We track two variables at every index i: currentSum, which decides whether to extend the previous subarray or start fresh at nums[i], and maxSum, which records the global record seen so far.
Given: nums=[−2,1,−3,4,−1,2,1,−5,4]. We initialize both currentSum=−2 and maxSum=−2 using the first element.
Result: The maximum contiguous subarray sum is 6, generated by the slice [4,−1,2,1].
Try this: Trace the same step-by-step logic for the secondary locked array nums = [-1, -2, -3]. What will maxSum be after processing the final element -3?
Tracing Kadane's Algorithm Step by Step on [-2, 1, -3, 4, -1, 2, 1, -5, 4] diagram
Practice
Putting Kadane's Algorithm to the Test on All-Negative Inputs
Now that you have seen how Kadane's algorithm traces through the mixed sequence [−2,1,−3,4,−1,2,1,−5,4] to find a maximum sum of 6, it is time to test your grasp of the update rules on the second half of our locked example: the all-negative sequence nums=[−1,−2,−3].
Recall that cur=max(x,cur+x) and best=max(best,cur). If you initialize cur and best to the first element instead of zero, you avoid the trap of returning an empty subarray sum of 0.
Work through the array [−1,−2,−3] element by element, tracking what cur and best become after each step.
Try this: Given nums = [-1, -2, -3].
1.Initialize: cur = nums[0], best = nums[0]
2.Step 1 (x = -2): cur = max(-2, cur + (-2)), best = max(best, cur)
3.Step 2 (x = -3): cur = max(-3, cur + (-3)), best = max(best, cur)
What are the final values of cur and best?
Apply
Transferring Kadane's Thinking to Peak Profit and Stream Processing
The pattern we uncovered while tracking [−2,1,−3,4,−1,2,1,−5,4] and [−1,−2,−3] is not just for finding contiguous sums in static arrays. Whenever you need to find a maximum contiguous window where local decisions either anchor a new start or extend an existing run, this exact dynamic programming skeleton applies. For instance, consider finding the maximum profit from daily price changes instead of raw numbers, or processing an infinite real-time telemetry stream where memory is bounded and you can only keep running counters. The core insight—that a negative running accumulator should be dropped because it hurts any future contribution—translates directly to any domain dealing with local drag versus explosive upside.
What is the maximum subarray sum for [-2, 1, -3, 4, -1, 2, 1, -5, 4] using Kadane's algorithm?
The maximum sum is 6, generated by the subarray [4, -1, 2, 1].
How does Kadane's algorithm handle arrays where all numbers are negative, like [-1, -2, -3]?
Kadane's algorithm correctly returns the maximum single element, which is -1, rather than defaulting to 0, provided the current max and global max are initialized to the first element rather than 0.
What is the time and space complexity of Kadane's algorithm?
Kadane's algorithm runs in O(n) time with a single pass through the array and O(1) auxiliary space, making it optimal for the maximum subarray problem.