Max contiguous subarray in Python

Problem statement:

In max contiguous subarray in Python, you are given a list of integers(positive and negative), and you have to find a sub-list from the given list which has the maximum sum and print the sum.

For example: list = [1, 6, -7, 5]
now, possible contiguous lists are:
[1, 6]
[1, 6, -7]
[1, 6, -7, 5]
[6, -7]
[6, -7, 5]
[-7, 5]

Now, the list with the highest sum is the 2nd list having 7 as the total sum.

Now, let’s code the max sub-array problem.

Code for Max contiguous subarray in Python:

myList = [-1, 2, 4, -7, 5, -9]
listOfSubLists = []
for i in range(len(myList)+1):
    for j in range(i+1, len(myList)+1):
listOfAllSum = []
for i in listOfSubLists:
max = -1
index = 0
for i in range(len(listOfAllSum)):
    if listOfAllSum[i]>max:
        max = listOfAllSum[i]
        index = i
print('List with maximum sum is: ', listOfSubLists[index])
print('Maximum sum is: ', max)


Max contiguous subarray in Python

Also Read:


Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at Thank you