Learner Level
Implement the maximumAverageSubarrayFloor method that returns the floor of the maximum average of a fixed-length subarray.
The input contains an integer array nums, its length size, and a window size k. Your task is to find the maximum average value among all continuous subarrays of length k.
The method should return the floor value of the maximum average.
For example, for [1,12,-5,-6,50,3] with k = 4, the maximum sum is 51, the average is 12.75, and the returned value is 12.
Use a fixed-size sliding window of length k.
First calculate the sum of the first k elements. Then slide the window one position at a time by adding the new right value and removing the old left value.
Track the largest window sum. At the end, divide it by k and return the floor of that average.
Pseudocode:
function maximumAverageSubarrayFloor(nums, size, k):
if k <= 0 or size < k:
return 0
windowSum = 0
for i from 0 to k - 1:
windowSum = windowSum + nums[i]
bestSum = windowSum
for right from k to size - 1:
windowSum = windowSum + nums[right] - nums[right - k]
bestSum = maximum(bestSum, windowSum)
return floor(bestSum / k)