Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.
Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.
Solutions
🐍 Python
def maxSequence(arr):
if not arr:
return 0
#first_positive
fp, fp_n = -1, 0
for n, x in enumerate(arr):
if x <= 0 and fp == -1:
continue
elif fp == -1:
fp = x
fp_n = n
#quality_len
ql = len(arr[fp_n:])
#max sum
ms = 0
for n1 in range(ql):
for n2 in range(1, ql - n1 + 1):
if sum( arr[fp_n + n1:fp_n + n1 + n2] ) > ms:
ms = sum( arr[fp_n + n1:fp_n + n1 +n2] )
return ms