Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save basekays/90df32c7bede71f2892a613ee4c125ce to your computer and use it in GitHub Desktop.
Save basekays/90df32c7bede71f2892a613ee4c125ce to your computer and use it in GitHub Desktop.
function maxRangeSum(string) {
const splitString = string.split(" ");
splitString.shift();
const gainsLossesArray = [];
for (let i = 0; i < splitString.length; i++) {
gainsLossesArray.push(parseInt(splitString[i]));
}
let currentMax = 0;
let maxGain = 0;
for (let i = 0; i < gainsLossesArray.length; i++) {
currentMax = Math.max(0, currentMax + gainsLossesArray[i]);
maxGain = Math.max(maxGain, currentMax);
}
return maxGain;
}
maxRangeSum('10 7 -3 -10 4 2 8 -2 4 -5 -6');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment