Answer to Question #310871 in Python for Fe 3

Question #310871

Max contiguous subarray




Input



[2,-4 ,5 ,-1,2,-3]





1
Expert's answer
2022-03-16T10:24:53-0400
def max_Subarray_Sum(my_array):
    maxValue = float('-inf')
    maxPoint = 0
    start = end = s = 0
    
    for i in range(len(my_array)):
        maxPoint += my_array[i]
        if(maxValue < maxPoint):
            maxValue = maxPoint
            start = s
            end = i
        if(maxPoint < 0):
            maxPoint = 0
            s = i + 1
    
    print("Sub-Array starting from %(1)d to %(2)d has a largest sum of %(3)d"
        %{'1':start, '2':end, '3':maxValue})

my_array = [2,-4 ,5 ,-1,2,-3]
max_Subarray_Sum(my_array)

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