5 Best Ways to Find the Maximum Sum of a Contiguous Sublist in Python

Rate this post

💡 Problem Formulation: The challenge is to find the maximum sum of a contiguous subset within a list of numbers. This is commonly known as the Maximum Subarray Problem or Kadane’s algorithm. For example, given the input list [-2, -3, 4, -1, -2, 1, 5, -3], the contiguous sublist with the maximum sum is [4, -1, -2, 1, 5], and the desired output is 7.

Method 1: Naive Approach

The naive approach involves generating all possible sublists of the given list and calculating their sums to find the maximum sum. It is simple but not optimal with time complexity O(n^3).

Here’s an example:

def max_sublist_sum_naive(arr):
    max_sum = arr[0]
    n = len(arr)
    for i in range(n):
        for j in range(i, n):
            current_sum = sum(arr[i:j+1])
            max_sum = max(max_sum, current_sum)
    return max_sum

array = [-2, -3, 4, -1, -2, 1, 5, -3]

Output: 7

This code snippet calculates the maximum sum by iterating through all the sublists, summing their elements, and keeping track of the maximum sum encountered. It’s a brute-force method that is easy to understand, but it can be very slow for large lists.

Method 2: Kadane’s Algorithm

Kadane’s Algorithm is a dynamic programming technique that solves this problem in linear time, O(n). It iteratively computes the maximum subarray sum ending at each position by reusing previous results, maintaining global and local maxima.

Here’s an example:

def kadanes_algorithm(arr):
    max_ending_here = max_so_far = arr[0]
    for x in arr[1:]:
        max_ending_here = max(x, max_ending_here + x)
        max_so_far = max(max_so_far, max_ending_here)
    return max_so_far

array = [-2, -3, 4, -1, -2, 1, 5, -3]

Output: 7

The code snippet applies Kadane’s algorithm, which is efficient and the preferred solution for this problem. The function scans the list left to right, updating the maximum sum of any subarray ending at each index. This method uses optimal substructure and overlapping subproblems, making it an elegant and efficient solution.

Method 3: Divide and Conquer

Divide and conquer is a recursive approach that splits the list into halves and finds the maximum subarray sum within each half and across the middle. It combines these to find the global maximum. It operates in O(n log n) time complexity.

Here’s an example:

def max_crossing_sum(arr, l, m, h):
    left_sum = float('-inf')
    right_sum = float('-inf')
    sum = 0
    for i in range(m, l-1, -1):
        sum += arr[i]
        if (sum > left_sum):
            left_sum = sum
    sum = 0
    for i in range(m + 1, h + 1):
        sum += arr[i]
        if (sum > right_sum):
            right_sum = sum
    return max(left_sum + right_sum, left_sum, right_sum)

def max_sublist_sum_divide_conquer(arr, l, h):
    if l == h:
        return arr[l]
    m = (l + h) // 2
    return max(max_sublist_sum_divide_conquer(arr, l, m),
               max_sublist_sum_divide_conquer(arr, m+1, h),
               max_crossing_sum(arr, l, m, h))

array = [-2, -3, 4, -1, -2, 1, 5, -3]
print(max_sublist_sum_divide_conquer(array, 0, len(array) - 1))

Output: 7

This code snippet employs a divide and conquer strategy, where the problem is divided into smaller subproblems that are solved independently. It calculates the maximum sum of a subarray that crosses the midpoint and combines it with the maximum sums of the left and right halves. While more complex than Kadane’s algorithm, it is useful for understanding recursive problem-solving in computational theory.

Method 4: Dynamic Programming

Dynamic programming optimizes the naive approach by storing intermediate results to avoid redundant calculations. The algorithm keeps track of the maximum sum of subarrays ending at different positions, with a time complexity of O(n).

Here’s an example:

def max_sublist_sum_dp(arr):
    n = len(arr)
    dp = [0] * n
    dp[0] = arr[0]
    max_sum = dp[0]
    for i in range(1, n):
        dp[i] = max(arr[i], dp[i-1] + arr[i])
        max_sum = max(max_sum, dp[i])
    return max_sum

array = [-2, -3, 4, -1, -2, 1, 5, -3]

Output: 7

This dynamic programming approach reduces the time complexity by avoiding repeated work done in the naive approach. It computes the maximum sum by optimally combining the subproblems using previously calculated results stored in a table (array). This method uses the fundamental concept of dynamic programming, which is solving more complex problems by combining the solutions of simpler subproblems.

Bonus One-Liner Method 5: Pythonic Way

For those who appreciate Python’s brevity, here’s a one-liner using functools and itertools to compute the maximum sum of a contiguous sublist in a functional style. This method is less efficient than Kadane’s algorithm but showcases Python’s ability to express complex operations succinctly.

Here’s an example:

from itertools import accumulate
import functools

array = [-2, -3, 4, -1, -2, 1, 5, -3]
print(functools.reduce(lambda x, y: max(x + y, y), array, 0))

Output: 7

This single line of code uses the functools.reduce() function alongside itertools.accumulate() to reduce the array to its maximum sublist sum. This functional approach translates the operation into a compact form while retaining the core concept, but it may sacrifice some readability and performance.


  • Method 1: Naive Approach. Simple to understand but inefficient with time complexity O(n^3).
  • Method 2: Kadane’s Algorithm. Highly efficient with linear time complexity O(n). Optimal for practical use and widely accepted solution for this problem.
  • Method 3: Divide and Conquer. Logarithmic time complexity O(n log n). Offers a good exercise in understanding recursive problem-solving though not as efficient as Kadane’s.
  • Method 4: Dynamic Programming. Linear time complexity O(n). Similar to Kadane’s but uses additional space to store intermediate results, suitable for problems where such historical data is needed.
  • Method 5: Pythonic One-Liner. It is concise and leverages Python’s functional programming features but isn’t suitable for large datasets due to efficiency concerns.