Problems178
Search for a command to run...
Fixed window of size k · find maximum average value
Try your own input
Change the array and replay the algorithm from step one.
Current step
Find the contiguous subarray of length k = 4 with the maximum average.
Sliding Window
1function findMaxAverage(nums, k) {//n: 6k: 4windowSum: —maxSum: —2 let windowSum = 0;3 for (let i = 0; i < k; i++) {4 windowSum += nums[i];5 }6 let maxSum = windowSum;7 for (let i = k; i < nums.length; i++) {8 windowSum = windowSum - nums[i - k] + nums[i];9 maxSum = Math.max(maxSum, windowSum);10 }11 return maxSum / k;12}