Complete the code to slide the window by moving the start index forward.
int start = 0; for (int end = 0; end < nums.Length; end++) { if (end - start + 1 > k) { start = [1]; } }
To slide the window forward, we increase the start index by 1.
Complete the code to calculate the sum of the current sliding window.
int windowSum = 0; for (int i = 0; i < k; i++) { windowSum += [1]; }
We add the element at index i of the array to the window sum.
Fix the error in updating the window sum when sliding the window forward.
windowSum += nums[end];
windowSum -= [1];We subtract the element at the start index to remove it from the window sum.
Fill both blanks to create a sliding window that finds the maximum sum of any window of size k.
int maxSum = 0; int windowSum = 0; int start = 0; for (int end = 0; end < nums.Length; end++) { windowSum += nums[end]; if (end >= [1] - 1) { maxSum = Math.Max(maxSum, windowSum); windowSum -= nums[[2]]; start++; } }
We start checking sums when the window size reaches k, then subtract the element at start to slide the window.
Fill all three blanks to implement a sliding window that returns the average of each window of size k.
List<double> averages = new List<double>(); int windowSum = 0; int start = 0; for (int end = 0; end < nums.Length; end++) { windowSum += nums[end]; if (end >= [1] - 1) { averages.Add(windowSum / (double)[2]); windowSum -= nums[[3]]; start++; } }
We start calculating averages when the window size reaches k, divide by k for average, and subtract nums[start] to slide the window.
