Compute the average of every subarray centered at each index with radius k, or -1 when the centered window does not fit.
Given an integer array nums and an integer k, define the k-radius average at index i as the average of the subarray centered at i with length 2k + 1:
nums[i-k ... i+k]i is -1Return an array averages where averages[i] is the k-radius average of nums centered at index i, using integer division by the window length 2k + 1.
-1.numsknums.i, output the average of the centered window of size 2k+1 if it exists, otherwise -1.1 <= nums.length0 <= ki-k >= 0 and i+k < nums.lengthnums: integer arrayk: non-negative integeraverages of length nums.length2k + 1-1 for indices without a full windowExample 1
Input
nums = [7,4,3,9,1,8,5,2,6], k = 3
Output
[-1,-1,-1,5,4,4,-1,-1,-1]
Explanation
The centered window has length 7. For index 3, the sum of [7,4,3,9,1,8,5] is 37, and 37 / 7 = 5. Similarly, index 4 gives 31 / 7 = 4, and index 5 gives 28 / 7 = 4. The other positions do not have enough elements on both sides.
Example 2
Input
nums = [100000], k = 0
Output
[100000]
Explanation
A window of length 1 centered at index 0 is valid, so the average is the element itself.
Example 3
Input
nums = [8,8,8,8], k = 1
Output
[-1,8,8,-1]
Explanation
Only indices 1 and 2 have a full window of length 3. Each window sums to 24, and 24 / 3 = 8.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.