Elixir Max Slice Sum
defmodule MaxSliceSum do
def max_slice_sum([first | rest]) do
{_tmp, max} =
Enum.reduce(rest, {first, first}, fn v, {tmp, max} ->
tmp = max(tmp + v, v)
{tmp, max(max, tmp)}
end)
max
end
end
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.