Created
May 8, 2014 19:12
-
-
Save jdrury/a3c8fc63ae1e0c076dff to your computer and use it in GitHub Desktop.
maximum contiguous 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
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