Last active
December 23, 2015 16:39
-
-
Save wszdwp/6663459 to your computer and use it in GitHub Desktop.
Crack the interview 19.7
This file contains 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 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