Answer to Question #174683 in Python for bhuvanesh

Question #174683

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.


1
Expert's answer
2021-03-27T02:51:18-0400
def largest_sum_contiguous_sublist(x):
    """
    Parameters:
        x: list of integers
    Returns:
        the largest sum of x sublist elements
    """
    glob_max = 0
    loc_max = 0
    for elem in x:
        loc_max += elem
        if loc_max < 0:
            loc_max = 0
        elif glob_max < loc_max:
            glob_max = loc_max
    return glob_max

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