Skip to content

Instantly share code, notes, and snippets.

@jdrury
Created May 8, 2014 19:12
Show Gist options
  • Save jdrury/a3c8fc63ae1e0c076dff to your computer and use it in GitHub Desktop.
Save jdrury/a3c8fc63ae1e0c076dff to your computer and use it in GitHub Desktop.
maximum contiguous sum
function add (collection){
var sum = 0;
collection.forEach(function(number) {
sum += number;
});
return sum;
}
function maxContig (collection){
var sum = 0;
var index;
for (var i = 0; i < collection.length; i++){
for (var j = 0; j < collection.length; j++) {
var rangeSum = add(collection.slice(i,j));
if (rangeSum > sum) {
sum = rangeSum;
index = [i,j];
}
}
}
return collection.slice(index[0],index[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment