Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created July 11, 2016 15:58
Show Gist options
  • Save tiagopereira17/ae6082d9d37b1ce2c520e049f58b60b2 to your computer and use it in GitHub Desktop.
Save tiagopereira17/ae6082d9d37b1ce2c520e049f58b60b2 to your computer and use it in GitHub Desktop.
Find a maximum sum of a compact subsequence of array elements.
public class MaxSliceSum {
public int maxSliceSum(int[] A) {
if(A == null || A.length == 0) {
return 0;
}
int maxSum = Integer.MIN_VALUE;
int tempMaxSum = 0;
for(int i = 0; i < A.length; i++) {
tempMaxSum = Math.max(A[i], A[i] + tempMaxSum);
maxSum = Math.max(tempMaxSum, maxSum);
}
return maxSum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment