K Radius Subarray Averages (LeetCode 2090): Solution and Explanation

Medium
Array
Sliding window

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

  1. Let width = 2k + 1 and fill avgs with -1.
  2. For each i, add nums[i] to a 64-bit running sum.
  3. If i >= width, subtract nums[i - width], the element that just fell off the left.
  4. If i >= width - 1, the window is full: set avgs[i - k] = sum // width.
  5. Return avgs.

Step-by-step example

nums = [7,4,3,9,1,8,5,2,6], k = 3
  1. width = 7. Indices 0 to 5 only fill the window; the sum reaches 32 after index 5.
  2. i = 6: sum = 37, the window 0..6 is full, so avgs[3] = 37 // 7 = 5.
  3. i = 7: add 2, drop nums[0] = 7, sum = 32, avgs[4] = 32 // 7 = 4.
  4. i = 8: add 6, drop nums[1] = 4, sum = 34, avgs[5] = 34 // 7 = 4.
  5. 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 avgs
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each element is added once and removed at most once.

Space

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 of nums[i - width], which leaves the window one element short.
  • Writing the average at index i instead of the center i - 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.