Last active
August 29, 2015 14:13
-
-
Save dmnugent80/6e048f639096f118a16d to your computer and use it in GitHub Desktop.
Given a list of numbers, find the contiguous sublist that has the largest sum.
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 int getSumLargestSubset(int[] array){ | |
int sumSoFar = 0; | |
int largestSum = 0; | |
for (int i=0; i < array.length; i++){ | |
sumSoFar = sumSoFar + array[i]; | |
if (sumSoFar < 0){ | |
sumSoFar = 0; | |
} | |
else if (sumSoFar > largestSum){ | |
largestSum = sumSoFar; | |
} | |
} | |
return largestSum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment