Max contiguous subarray
Input
[2,-4 ,5 ,-1,2,-3]
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)
Comments
Leave a comment