Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Last active August 29, 2015 14:13
Show Gist options
  • Save dmnugent80/6e048f639096f118a16d to your computer and use it in GitHub Desktop.
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.
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