Skip to content

Instantly share code, notes, and snippets.

@dashsaurabh
Created January 20, 2020 11:30
Show Gist options
  • Save dashsaurabh/5118f20085f2c6f114bde7e469d6bfc6 to your computer and use it in GitHub Desktop.
Save dashsaurabh/5118f20085f2c6f114bde7e469d6bfc6 to your computer and use it in GitHub Desktop.
Find Max SubArray from an Array
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let globalMax = nums[0];
let localMax = nums[0];
for(let i=1;i<nums.length;i++) {
localMax = Math.max(localMax+nums[i], nums[i])
globalMax = Math.max(globalMax, localMax);
}
return globalMax;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment