Skip to content

Instantly share code, notes, and snippets.

@kharioki
Created May 24, 2021 15:08
Show Gist options
  • Save kharioki/2d9bba0090a110ad2ff7c2b76f464b3f to your computer and use it in GitHub Desktop.
Save kharioki/2d9bba0090a110ad2ff7c2b76f464b3f to your computer and use it in GitHub Desktop.
Finding the maximum subarray.
let arr1 = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7];
function maxSubArray (nums) {
let maxC = nums[0];
let maxG = nums[0];
for (let i = 1; i < nums.length; i++) {
maxC = Math.max(nums[i], maxC + nums[i]);
maxG = maxC > maxG ? maxC : maxG;
}
// console.log(maxG);
return maxG;
}
maxSubArray(arr1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment