Skip to content

Instantly share code, notes, and snippets.

@oskar-j
Created July 28, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskar-j/b0ec43655d09c0d7affd to your computer and use it in GitHub Desktop.
Save oskar-j/b0ec43655d09c0d7affd to your computer and use it in GitHub Desktop.
Find maximum sum in slices (subsequences) of an array in Java
public class MaxSliceSum {
public int solution(int[] A) {
int ans = A[0], sum = 0;
for (int i = 0; i < A.length; i++) {
if (sum > 0) {
sum += A[i];
} else {
sum = A[i];
}
ans = Math.max(ans, sum);
}
return ans;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment