const maxSubArray = (nums) => {
// initiate two variable, maxSum for total max, sum for current max
let maxSum = -Infinity
let currentSum = 0
// iterate through the nums, store sub-problems result
for(let i = 0; i < nums.length; i++){
//cumulating answers to the top
//compare currentSum add current number
//with current number and store the maximum value
currentSum = Math.max(nums[i], currentSum + nums[i])
//compare maxSum with currentSum and store the greater value
maxSum = Math.max(currentSum, maxSum)
}
return maxSum
}
function maxSumArr(arr, num) {
let maxSum = 0;
let tempSum = 0;
if(arr.length < num) return null;
for(let i = 0; i < num; i++) {
tempSum += arr[i];
}
tempSum = maxSum;
for(let i = num; i < arr.length; i++) {
tempSum = tempSum - arr[i - num] + arr[i];
maxSum = Math.max(tempSum, maxSum);
}
return maxSum;
}