package offer;
/**
* offer interview 31
*/
public class Test31 {
public static int findGreatestSumOfSubArray(int[] arr){
if (arr == null || arr.length < 1){
throw new IllegalArgumentException("Array must contain an element.");
}
int max = Integer.MIN_VALUE;
int curMax = 0;
for (int i : arr){
if (curMax <= 0){
curMax = i;
}else{
curMax += i;
}
if (max < curMax){
max = curMax;
}
}
return max;
}
public static void main(String[] args) {
int[] data = {1, -2, 3, 10, -4, 7, 2, -5};
int[] data2 = {-2, -8, -1, -5, -9};
int[] data3 = {2, 8, 1, 5, 9};
System.out.println(findGreatestSumOfSubArray(data));
System.out.println(findGreatestSumOfSubArray(data2));
System.out.println(findGreatestSumOfSubArray(data3));
}
}