JS | Largest Sum Contiguous Subarray | Kaden’s Algorithm | Dynamic Programing | O(n)

Array = [-2, -3, 4, -1, -2, 1, 5, -3]var maxSubArray = function(nums) {
let sum = 0;
let maxSum = -Infinity;

if(nums.length === 0) return 0;
if(nums.length === 1) return nums[0]

for(let i = 0;i<nums.length;i++){
sum+=nums[i];
maxSum = Math.max(maxSum,sum);
if(sum < 0) sum = 0;
}
return maxSum;
};
function maxSubArraySum(a,size) {   let max_so_far = a[0];
let curr_max = a[0];
for (let i = 1; i < size; i++) {
curr_max = Math.max(a[i], curr_max + a[i]);
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store