◨◧ write function maxOfSubArraySum which accepts first param array and second param as number,sencond param tells that how many elements should counts and travel array find sum of sub array,
Silding window pattern ◨◧
Following is sample javascript code.
// silding window pattern
function maxOfSubArraySum(arr, num) {
let temp = 0;
let max = 0;
for(let i= 0; i < num; i++){
temp += arr[i]
// console.log(temp)
}
max = temp;
for(let j = num; j < arr.length; j++) {
temp = temp - arr[j-num] + arr[j];
// console.log(temp, max)
max = Math.max(temp, max)
}
return max
}
input
console.log(maxOfSubArraySum([1,1,1,9,1,5,5], 3))
output
15
input
console.log(maxOfSubArraySum([1,1,1,2,3,4,1,1,1,5,6,8,8,9,9,10], 3))
output
28