Skip to content

Instantly share code, notes, and snippets.

@wusuopubupt
Forked from czxttkl/gist:7929509
Created December 13, 2013 03:38
Show Gist options
  • Save wusuopubupt/7939464 to your computer and use it in GitHub Desktop.
Save wusuopubupt/7939464 to your computer and use it in GitHub Desktop.
public class TestConSum {
public static void main(String... args) {
int[] arr = new int[]{-2,11,-4,13,-5,-2};
int negsum = 0;
int maxsum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
negsum += arr[i];
continue;
}
if (arr[i] > 0) {
maxsum = max(maxsum + negsum + arr[i], arr[i]);
negsum = 0;
}
}
System.out.println(maxsum);
}
private static int max (int a, int b) {
return a > b? a : b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment