Last active
December 14, 2015 09:58
-
-
Save charlespunk/5068619 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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