Concepts
Fixed-size rolling sum window
Reuse a rolling aggregate over a fixed number of consecutive elements. This sum technique assumes a valid positive window length and bounded arithmetic.
Formal Syntax & Structure
javascript syntaxjavascript
maxWindow(values, k) → maximum sumWorking Example
Example Snippetjavascript
const a=[2,5,-1,4],k=2;let sum=a[0]+a[1];const sums=[sum];for(let i=k;i<a.length;i++){sum+=a[i]-a[i-k];sums.push(sum);}console.log(JSON.stringify(sums));