Skip to content

Instantly share code, notes, and snippets.

@IAMIronmanSam
Created April 3, 2019 04:28
Show Gist options
  • Save IAMIronmanSam/aeba1ddf4f690657b3f61ef7fd2748cc to your computer and use it in GitHub Desktop.
Save IAMIronmanSam/aeba1ddf4f690657b3f61ef7fd2748cc to your computer and use it in GitHub Desktop.
function maxSubarraySum(arr, num){
let maxSum = 0;
let tempSum = 0;
if (arr.length < num) return null;
for (let i = 0; i < num; i++) {
maxSum += arr[i];
}
tempSum = maxSum;
for (let i = num; i < arr.length; i++) {
tempSum = tempSum - arr[i - num] + arr[i];
maxSum = Math.max(maxSum, tempSum);
}
return maxSum;
}
maxSubarraySum([2,6,9,2,1,8,5,6,3],3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment