Python | Maximum Sum Sublist Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report The task is to find a contiguous sublist (i.e., a sequence of elements that appear consecutively in the original list) such that the sum of the elements in this sublist is as large as possible. We need to return the maximum sum of this sublist. Let's explore methods to find Maximum Sum Sublist in python:Kadane’s Algorithm (Optimal Approach)ApproachKadane’s algorithm uses dynamic programming to solve the problem in a single pass. This approach involves:At each index, decide whether to start a new sublist or continue the existing sublist by adding the current element.Keep track of the best (maximum) sum found so far.Code: Python def kadane(arr): max_sum = float('-inf') cur_sum = 0 for x in arr: # Either add x to the existing subarray or start fresh from x cur_sum = max(x, cur_sum + x) # Update global maximum max_sum = max(max_sum, cur_sum) return max_sum # Example usage: if __name__ == "__main__": arr = [1, -3, 2, 1, -1] print(kadane(arr)) Output3 Explanation:Dynamic Programming: Use a variable (cur_Sum) to keep track of the best sublist ending at the current position.Choose or Restart: At each element x, decide whether to add x to cur_Sumor start a new sum at x.Global Maximum: Keep a separate max_sum that stores the maximum sum encountered so far.Time Complexity: O(n)Using For Loop (Brute Force Approach)This method's approach involves evaluating the sum of every possible sublist and keeping track of the maximum sum found. While this method is easy to understand, it is highly inefficient for large lists due to its cubic time complexity. Python def max_subarray(arr): n = len(arr) max_sum = float('-inf') for start in range(n): cur_sum = 0 for end in range(start, n): cur_sum += arr[end] # accumulate sum for subarray [start..end] if cur_sum > max_sum: max_sum = cur_sum return max_sum # Example Usage if __name__ == "__main__": arr = [1, -3, 2, 1, -1] print(max_subarray(arr)) Output3 Explanation:Nested Loops: We use two loops to explore all possible contiguous sublists.Compute Sublist Sum: Inside the inner loop, we continuously add elements to a running sum.Track Global Max: We update a max_sum variable whenever the current sum exceeds the previously recorded maximum.Time Complexity: O(n^2) Create Quiz Comment M manjeet_04 Follow 2 Improve M manjeet_04 Follow 2 Improve Article Tags : Python Python Programs python-list Python list-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like