I know how to find a maximum, contiguous, subsequence sum; but sometimes there's more than one subsequence with maximum sum. So, I need to find the index of those longest subsequences with the maximum sum. The only thing I thought of is brute force. What better options are there?
Here's a code I found on rosettacode which had the exact idea for my problem (but, sadly, the only programming language I know is Java), but it is writen in REXX:
/*───────────────────────────────────────────────────────────────*/
arg @
say 'words='words(@) 'list='@
say
sum=word(@,1)
w=words(@)
at=1
L=0
do j=1 for w; f=word(@,j)
do k=j to w; s=f
do m=j+1 to k
s=s+word(@,m)
end /*m*/
_=k-j+1
if (s==sum & _>L) | s>sum then do; sum=s; at=j; L=_; end
end /*k*/
end /*j*/
seq=subword(@,at,L)
if seq=='' then seq="[NULL]"
sum=word(sum 0,1)
say 'sum='sum/1 "sequence="seq
/*───────────────────────────────────────────────────────────────*/
Results:
input 1 2 3 4 -777 1 2 3 4 0 0
output
words=12 list=1 2 3 4 0 -777 1 2 3 4 0 0
sum=10 sequence=1 2 3 4 0 0
1 2 3 4 0 0is a contiguous subsequence. Could you define contiguous subsequence for us?1 2 3 4 0 0found consecutively within the list? That would make it a contiguous subsequence. What's confusing about it?-777 1 2 3 4 0 0isn't picked, since it's in list. What defines contiguous in this sense?