// [ ] {4} 5
//
// [4, 7]
// m = 4
// curr = 7
// max = 4
// => max = 7
// ---------------
// [4, 7, 5]
// m = 4
// curr = 7
// max = 4
// ------
// m = 7
// curr = 4 + 5
// max = 7
// => max(9, 7) = 9
// ---------------
public static int maxNonConsecutiveSum(int[] arr){
int max = 0;
if(arr != null && arr.length > 0){
int len = arr.length;
int pmax = 0;
max = arr[0];
for(int i=1; i<len; i++){
int m1 = Math.max(pmax + arr[i], arr[i]);
int m2 = Math.max(max, arr[i]);
pmax = max;
max = Math.max(m1, m2);
}
}
return max;
}
// 4, -5, 9, 7, -8, 3
//--------------------------------------------------------------------------------
// m = 0
// max = 0
//--------------------------------------------------------------------------------
// m = max(m, m+3) = 3
// max = 3
//--------------------------------------------------------------------------------
// m = max(m, m+-8) = -5 = 0
// max = 3
//--------------------------------------------------------------------------------
// m = max(m, m+7) = 7
// max = 7
//--------------------------------------------------------------------------------
// m = max(m, m+9) = 16
// max = 16
//--------------------------------------------------------------------------------
// m = max(m, m -5) = 11
// max = 16
//--------------------------------------------------------------------------------
// m = max(m, m + 4) = 15
// max = 16
//--------------------------------------------------------------------------------
public static int maxConsecutive(int[] arr) {
int m = 0;
int max = 0;
if(arr != null) {
for(int i=0; i<arr.length; i++) {
if(m + arr[i] > 0)
m = Math.max(m + arr[i], arr[i]);
else
m = 0;
if(m > max)
max = m;
}
}
return max;
}