C++ Max Slice Sum
#include <algorithm>
#include <limits>
#include <vector>
long long maxSliceSum(const std::vector<int>& a)
{
long long tmp = std::numeric_limits<long long>::min();
long long max = std::numeric_limits<long long>::min();
for (int v : a) {
tmp = std::max(tmp + v, static_cast<long long>(v));
max = std::max(max, tmp);
}
return max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
C# Max Slice Sum
static long MaxSliceSum(int[] a)
{
long tmp = long.MinValue;
long max = long.MinValue;
foreach (var v in a)
{
tmp = Math.Max(tmp + v, v);
max = Math.Max(max, tmp);
}
return max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
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.
Erlang Max Slice Sum
-module(max_slice_sum).
-export([max_slice_sum/1]).
max_slice_sum([H | T]) ->
{_, Max} = lists:foldl(fun(V, {Tmp, MaxV}) ->
Tmp1 = max(Tmp + V, V),
{Tmp1, max(MaxV, Tmp1)}
end, {H, H}, T),
Max.
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Go Max Slice Sum
func maxSliceSum(a []int) int {
tmp, best := math.MinInt, math.MinInt
for _, v := range a {
if tmp+v > v {
tmp += v
} else {
tmp = v
}
if tmp > best {
best = tmp
}
}
return best
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Haskell Max Slice Sum
maxSliceSum :: [Int] -> Int
maxSliceSum a = snd (foldl step (minBound, minBound) a)
where
step (tmp, best) v =
let tmp' = max (tmp + v) v
in (tmp', max best tmp')
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Java Max Slice Sum
public class Solution {
public static int maxSliceSum(int[] a) {
long tmp = Long.MIN_VALUE;
long max = Long.MIN_VALUE;
for (int v : a) {
tmp = Math.max(tmp + v, v);
max = Math.max(max, tmp);
}
return (int) max;
}
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Lisp Max Slice Sum
(defun max-slice-sum (a)
(let ((tmp most-negative-fixnum)
(max most-negative-fixnum))
(dolist (v a)
(setf tmp (max (+ tmp v) v))
(setf max (max max tmp)))
max))
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
PHP Max Slice Sum
function maxSliceSum(array $a): int
{
$tmp = $max = PHP_INT_MIN;
foreach ($a as $v) {
$tmp = max($tmp + $v, $v);
$max = max($max, $tmp);
}
return $max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Python Max Slice Sum
def max_slice_sum(a: list[int]) -> int:
tmp = max_sum = float("-inf")
for v in a:
tmp = max(tmp + v, v)
max_sum = max(max_sum, tmp)
return int(max_sum)
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.