1const getLargestSumOfFiveConsecutiveElements = (arr) => {
2 let currSum = getSum(arr, 0, 4);
3 let largestSum = currSum;
4
5 for (let i = 1; i <= arr.length - 5; i++) {
6 currSum -= arr[i - 1]; // subtract element to the left of curr window
7 currSum += arr[i + 4]; // add last element in curr window
8 largestSum = Math.max(largestSum, currSum);
9 }
10
11 return largestSum;
12};
13
14const getSum = (arr, start, end) => {
15 let sum = 0;
16
17 for (let i = start; i <= end; i++) {
18 sum += arr[i];
19 }
20
21 return sum;
22};
23
1def window(iterable, size=2):
2 i = iter(iterable)
3 win = []
4 for e in range(0, size):
5 win.append(next(i))
6 yield win
7 for e in i:
8 win = win[1:] + [e]
9 yield win