K Radius Subarray Averages (LeetCode 2090): Solution and Explanation
Each valid center averages a window of exactly 2k + 1 elements. Slide one running sum across the array, write the average for the center i - k whenever the window is full, and leave -1 at the edges.
Intuition
A center c needs k elements on each side, so its window is nums[c - k..c + k], always 2k + 1 long. Centers within k of either end simply cannot have one, which is why they stay -1.
Next to each other, two centers' windows differ by one element on each end. Recomputing each window would cost O(k) per center; a running sum costs O(1).
It is easiest to drive the loop by the right edge i. When i enters, the window covers i - 2k..i and its center is i - k.
Approach: Fixed-size sliding window
- Let
width = 2k + 1and fillavgswith -1. - For each
i, addnums[i]to a 64-bit running sum. - If
i >= width, subtractnums[i - width], the element that just fell off the left. - If
i >= width - 1, the window is full: setavgs[i - k] = sum // width. - Return
avgs.
Step-by-step example
nums = [7,4,3,9,1,8,5,2,6], k = 3
- width = 7. Indices 0 to 5 only fill the window; the sum reaches 32 after index 5.
- i = 6: sum = 37, the window 0..6 is full, so avgs[3] = 37 // 7 = 5.
- i = 7: add 2, drop nums[0] = 7, sum = 32, avgs[4] = 32 // 7 = 4.
- i = 8: add 6, drop nums[1] = 4, sum = 34, avgs[5] = 34 // 7 = 4.
- Indices 0 to 2 and 6 to 8 keep -1. Answer: [-1,-1,-1,5,4,4,-1,-1,-1].
Code: Python, Java, C++
1class Solution:
2 def getAverages(self, nums: List[int], k: int) -> List[int]:
3 n = len(nums)
4 width = 2 * k + 1
5 avgs = [-1] * n
6 window = 0
7 for i in range(n):
8 window += nums[i]
9 if i >= width:
10 window -= nums[i - width]
11 if i >= width - 1:
12 avgs[i - k] = window // width
13 return avgs1class Solution {
2 public int[] getAverages(int[] nums, int k) {
3 int n = nums.length;
4 long width = 2L * k + 1;
5 int[] avgs = new int[n];
6 Arrays.fill(avgs, -1);
7 long window = 0;
8 for (int i = 0; i < n; i++) {
9 window += nums[i];
10 if (i >= width) {
11 window -= nums[i - (int) width];
12 }
13 if (i >= width - 1) {
14 avgs[i - k] = (int) (window / width);
15 }
16 }
17 return avgs;
18 }
19}1class Solution {
2public:
3 vector<int> getAverages(vector<int>& nums, int k) {
4 int n = nums.size();
5 long long width = 2LL * k + 1;
6 vector<int> avgs(n, -1);
7 long long window = 0;
8 for (int i = 0; i < n; i++) {
9 window += nums[i];
10 if (i >= width) {
11 window -= nums[i - width];
12 }
13 if (i >= width - 1) {
14 avgs[i - k] = (int) (window / width);
15 }
16 }
17 return avgs;
18 }
19};Time and space complexity
O(n)
Each element is added once and removed at most once.
O(1)
Apart from the output array, only the running sum and the width are stored.
Other approaches
Prefix sums (time O(n), space O(n))
Build 64-bit prefix sums and compute each center as (prefix[c + k + 1] - prefix[c - k]) // width. Same O(n) time, with an extra O(n) array.
Sum each window directly (time O(n * k), space O(1))
For each valid center, add its 2k + 1 elements. Easy to write but O(n * k), too slow when both reach 10^5.
Common mistakes
- Summing in a 32-bit int. A window of up to 2 * 10^5 + 1 elements, each up to 10^5, can reach about 2 * 10^10.
- Subtracting
nums[i - width + 1]instead ofnums[i - width], which leaves the window one element short. - Writing the average at index
iinstead of the centeri - k. - Using floating point division. The problem wants integer division, and doubles can round large sums incorrectly.
Frequently asked questions
What if 2k + 1 is larger than the array?
No center has enough neighbours, so the window never fills and every entry stays -1.
Why does the average itself fit in an int?
An average can never exceed the largest element, which is at most 10^5. Only the sum needs 64 bits.
Does k = 0 need special handling?
No. The width is 1, the window is a single element, and each average equals the element itself.
What is the time and space complexity?
O(n) time and O(1) extra space besides the output.