maximum sum sublist
sublist problem
algorithm
dynamic programming
computer science

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:

python
1def max_sublist_sum(nums):
2    if not nums:
3        raise ValueError("nums must not be empty")
4
5    best_ending_here = nums[0]
6    best_so_far = nums[0]
7
8    for x in nums[1:]:
9        best_ending_here = max(x, best_ending_here + x)
10        best_so_far = max(best_so_far, best_ending_here)
11
12    return best_so_far
13
14
15print(max_sublist_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4]))

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.

python
1def max_sublist(nums):
2    if not nums:
3        raise ValueError("nums must not be empty")
4
5    best_sum = nums[0]
6    current_sum = nums[0]
7    best_start = best_end = 0
8    current_start = 0
9
10    for i in range(1, len(nums)):
11        if nums[i] > current_sum + nums[i]:
12            current_sum = nums[i]
13            current_start = i
14        else:
15            current_sum += nums[i]
16
17        if current_sum > best_sum:
18            best_sum = current_sum
19            best_start = current_start
20            best_end = i
21
22    return best_sum, best_start, best_end
23
24
25nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
26print(max_sublist(nums))

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.

python
1def max_sublist_bruteforce(nums):
2    best = nums[0]
3    for i in range(len(nums)):
4        total = 0
5        for j in range(i, len(nums)):
6            total += nums[j]
7            best = max(best, total)
8    return best

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 and O(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.

Course illustration
Course illustration

All Rights Reserved.