Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Last active December 23, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wszdwp/6663459 to your computer and use it in GitHub Desktop.
Save wszdwp/6663459 to your computer and use it in GitHub Desktop.
Crack the interview 19.7
public class Question197
{
public static int maxsum(int[] a) {
int maxsum = 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
if( maxsum < sum) {
maxsum = sum;
} else if (sum < 0) {
sum = 0;
}
}
return maxsum;
}
public static void main(String[] args) {
int[] a1 = {2, -8, 3, -2, 4, -10};
int[] a2 = {2, -3, 8, -9};
System.out.println("Expected(5): " + maxsum(a1));
System.out.println("Expected(8): " + maxsum(a2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment