Maximum sum sublist?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The maximum sum sublist problem asks for the contiguous portion of a list whose elements add up to the largest possible value. In algorithm textbooks it is usually called the maximum subarray problem, and the standard linear-time solution is Kadane's algorithm.
Start with the Core Insight
For each position in the array, there are only two sensible choices:
- extend the best sublist that ended at the previous index
- start a new sublist at the current element
If the running sum has become worse than starting fresh, drop it. That local decision is enough to build the global optimum, which is why Kadane's algorithm runs in O(n) time.
Kadane's Algorithm in Python
Here is the classic implementation that returns only the best sum:
The output is 6, coming from the contiguous slice [4, -1, 2, 1].
Return the Actual Sublist Too
In interviews and production code, you often need the indices or the sublist itself, not just the sum. That requires tracking where the current candidate starts.
This returns the sum and the inclusive start and end positions of the best region.
Why Brute Force Is Usually the Wrong Baseline
A brute-force solution checks every possible contiguous sublist. With prefix sums you can reduce each sublist sum lookup, but the search still examines O(n^2) intervals.
This is fine for teaching or tiny arrays. It is not a serious choice once the input can grow large.
All-Negative Arrays Need Care
A common bug is initializing the best sum to 0. That fails when every value is negative, because the correct answer should be the least negative single element, not an empty sublist.
For example, with [-8, -3, -6], the answer is -3.
That is why robust implementations initialize from the first element rather than from zero.
Dynamic Programming Interpretation
Kadane's algorithm is often described as dynamic programming because it maintains a recurrence:
- best sum ending at index
i - best sum seen anywhere up to index
i
You do not store the whole DP table because each state depends only on the previous one. That makes the memory usage O(1).
Variants of the Problem
Once you understand the one-dimensional version, several related problems become easier:
- maximum circular subarray
- maximum product subarray
- maximum sum rectangle in a matrix
- best buy-sell window style problems
The key is always to understand what state must be carried forward and what can be discarded.
Common Pitfalls
The most common mistake is allowing an empty sublist implicitly by initializing the answer to zero. That breaks all-negative inputs.
Another mistake is confusing contiguous sublists with arbitrary subsequences. The maximum sum subsequence is a different problem because you can skip elements freely.
Developers also sometimes lose track of the indices when they restart the running sum. If you need the actual sublist, index bookkeeping must be updated at the same moment the running sum resets.
Finally, do not use the quadratic brute-force solution just because it feels easier to explain. Kadane's algorithm is short enough to be the default.
Summary
- The maximum sum sublist problem is the classic maximum subarray problem.
- Kadane's algorithm solves it in
O(n)time andO(1)space. - At each step, either extend the current sublist or start fresh at the current element.
- Initialize from the first element so all-negative inputs are handled correctly.
- Track start and end indices explicitly when you need the actual sublist, not only the sum.

