-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding-window.js
35 lines (28 loc) · 955 Bytes
/
sliding-window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function slidingWindow(arr, k) {
if (arr.length < k) {
return [];
}
let windowSum = 0;
let maxSum = 0;
let maxSumIndices = [];
// Compute sum of the first window
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
maxSum = windowSum;
// Slide the window by one element at a time
for (let i = k; i < arr.length; i++) {
// Add the next element to the window and subtract the first element of the previous window
windowSum += arr[i] - arr[i - k];
// Update maxSum if needed
if (windowSum > maxSum) {
maxSum = windowSum;
maxSumIndices = [i - k + 1, i]; // Store indices of the current maximum sum window
}
}
return maxSumIndices;
}
const arr = [4, 2, 1, 7, 8, 1, 2, 8, 1, 0];
const k = 3;
const maxSumIndices = slidingWindow(arr, k);
console.log("Indices of the maximum sum subarray of length", k, ":", maxSumIndices);