Skip to content

Instantly share code, notes, and snippets.

@sgoel
Last active August 29, 2015 14:22
Show Gist options
  • Save sgoel/1b543047b89a423dc137 to your computer and use it in GitHub Desktop.
Save sgoel/1b543047b89a423dc137 to your computer and use it in GitHub Desktop.
Maximum Subarray
var input = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
// Maximum Subarray
function maxiumumSubArray (inputArray) {
var reduced;
var max = 0;
for (var i = 0; i < input.length; i++) {
for (var j = i + 1; j < input.length; j++) {
reduced = input.slice(i, j).reduce(function(a, b) {
return a + b;
});
if (reduced > max) {
max = reduced;
}
}
}
return max;
};
console.log(maxiumumSubArray(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment