Write a Program to Find the Sum of Contiguous Subarray within a 1 – D Array of Numbers which has the Largest Sum.

If we are entering 8 elements (N = 8), with array element values as -1,-5,5,3,-2,5,4 and 1 then, The largest contiguous subarray is: 5 3 -2 5 4 1 The sum of the largest contiguous subarray is: 16 


nums=list(map(int,input().split()))
csum=nums[0]
msum=nums[0]
for i in range(1,len(nums)):
    csum=max(nums[i],csum+nums[i])
    msum=max(csum,msum)
print(msum)




 Input: 1 -5 5 3 -2 5 4 1
 Output: 16

No comments:

Post a Comment