Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list.Input
The input will contain space-separated integers, denoting the elements of the list.Output
The output should be an integer.
def max_sub_list(l:list):
if len(l) == 0:
print(0)
else:
max_sum = l[0]
for i in range(len(l)):
for j in range(i, len(l)):
if sum(l[j-i:j+1]) > max_sum:
max_sum = sum(l[j-i:j+1])
print(max_sum)
while True:
try:
arr = list(map(int,input().split()))
except ValueError:
continue
max_sub_list(arr)
Comments
Leave a comment