Maximum sum of subarray solution
def subarray(nums):
temp=nums[0]
result=nums[0]
for i in range(1,len(nums)):
temp=max(nums[i],temp+nums[i])
result=max(result,temp)
return result
nums=[-2,-3,4,-1,-2,1,5,-3]
print(subarray(nums))
output=7
def subarray(nums):
temp=nums[0]
result=nums[0]
for i in range(1,len(nums)):
temp=max(nums[i],temp+nums[i])
result=max(result,temp)
return result
nums=[-2,-3,4,-1,-2,1,5,-3]
print(subarray(nums))
output=7
Comments
Post a Comment
If you have any doubt you ask me in comment section