Last active
August 29, 2015 14:22
-
-
Save sgoel/1b543047b89a423dc137 to your computer and use it in GitHub Desktop.
Maximum Subarray
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
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