Skip to content

Instantly share code, notes, and snippets.

@michaellandi
Created September 19, 2016 04:12
Show Gist options
  • Save michaellandi/30566729fb97a8079b0cc41f330a244f to your computer and use it in GitHub Desktop.
Save michaellandi/30566729fb97a8079b0cc41f330a244f to your computer and use it in GitHub Desktop.
Find Largest Subarray (dynamic)
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