Answer to Question #206005 in Python for sudheer

Question #206005

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.


1
Expert's answer
2021-06-11T12:33:05-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS