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.