def maxsubarraysum(l):
    max = -1
    curr_max = 0
    start=0
    end=0
    x=[]
    for i in range(len(l)):
        curr_max = curr_max + l[i]
        if(max < curr_max):
            max = curr_max
            start = s
            end = i
        if(curr_max < 0):
            curr_max = 0
            s = i+1
    x.append(max)
    x.append(start)
    x.append(end)
    return x

l=[-2, -3, 4, -1, -2, 1, 5, -3]
ans = maxsubarraysum(l)
print("max sum :",ans[0])
print("the maximum subarray is ",end="")
print(l[ans[1]:ans[2]+1])

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: