prefix
of size n + 1
to store the prefix sums, with prefix[0] = 0
.prefix
array by setting prefix[i] = prefix[i - 1] + nums[i - 1]
for i
in range 1
to n
.averages
of size n
to store the k-radius averages, with all elements initially set to -1
.i
from k
to n - k - 1
.
a. Calculate the left and right bounds of the subarray as leftBound = i - k
and rightBound = i + k
.
b. Calculate the sum of the subarray as subarraySum = prefix[rightBound + 1] - prefix[leftBound]
.
c. Calculate the average as average = subarraySum / (2 * k + 1)
.
d. Assign average
to averages[i]
.averages
array.