RosettaCodeData/Task/Greatest-subsequential-sum/Julia/greatest-subsequential-sum.julia
2023-07-01 13:44:08 -04:00

17 lines
434 B
Text

function gss(arr::Vector{<:Number})
smax = hmax = tmax = 0
for head in eachindex(arr), tail in head:length(arr)
s = sum(arr[head:tail])
if s > smax
smax = s
hmax, tmax = head, tail
end
end
return arr[hmax:tmax]
end
arr = [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]
subseq = gss(arr)
s = sum(subseq)
println("Greatest subsequential sum of $arr:\n → $subseq with sum $s")