Bash Max Slice Sum
max_slice_sum() {
local -n _arr="$1"
local _tmp=-9223372036854775808 _max=-9223372036854775808
local _v
for _v in "${_arr[@]}"; do
if (( _tmp + _v > _v )); then _tmp=$(( _tmp + _v )); else _tmp=$_v; fi
if (( _tmp > _max )); then _max=$_tmp; fi
done
echo "$_max"
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.