Example Input: -1,2,6,4,-4,-5,56,78,-2,9
Desired Output: 134
public int MaxSumOfSubArray(int... input) {
int size = input.length;
int max = 0;
boolean isIn = false;
int current_sum = 0;
int previous = -1;
for(int i=0; i<size; i++) {
if(!isIn) current_sum = 0;
if(input[i]>=0) {
isIn = true;
current_sum = current_sum+input[i];
} else if(previous>=0 && max<current_sum) {
max = current_sum;
isIn = false;
}
previous = input[i];
}
return max;
}
No comments:
Post a Comment