Created
September 19, 2016 04:12
-
-
Save michaellandi/30566729fb97a8079b0cc41f330a244f to your computer and use it in GitHub Desktop.
Find Largest Subarray (dynamic)
This file contains hidden or 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 findLargestSubarray(arr) { | |
var result = {max:-1,start:-1,stop:-1}; | |
var row_data = []; | |
for (var i = 0; i < arr.length; i++) | |
for (var k = i; k < arr.length; k++) { | |
if (k == i) { | |
row_data.push(new Array(arr.length)); | |
row_data[i][k] = arr[i]; | |
} | |
else | |
row_data[i][k] = row_data[i][k - 1] + arr[k]; | |
if (row_data[i][k] > result.max) | |
result = {max:row_data[i][k],start:i,stop:k}; | |
} | |
console.log(row_data); | |
return result; | |
} | |
var arr = [20,-1,-1,-1,4]; | |
var result = findLargestSubarray(arr); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment