Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Last active December 14, 2015 09:58
Show Gist options
  • Save charlespunk/5068619 to your computer and use it in GitHub Desktop.
Save charlespunk/5068619 to your computer and use it in GitHub Desktop.
Find the biggest sum of a sequent integers.
For example:
[4 3 -2 1 -8 10 2 -3 -1]
The giggest sequence is "10 2" => 12.
public static int[] findBiggest(int[] input){
int begin = 0;
int end = 0;
int big = input[0];
int currentBegin = 0;
int currentEnd = 0;
int currentValue = input[0];
for(int i = 1; i < input.length; i++){
if(currentValue > 0){
currentValue += input[i];
currentEnd = i;
}
else{
currentBegin = i;
currentEnd = i;
currentValue = input[i];
}
if(currentValue > big){
begin = currentBegin;
end = currentEnd;
big = currentValue;
}
}
int[] result = new int[3];
result[0] = begin; result[1] = end; result[2] = big;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment