Ctrl K

Centered Window Averages

Medium
You are given an array of non-negative integers values and a number radius. For each position i, take the window of elements with indices from i - radius to i + radius inclusive and compute their average using integer division (the fractional part is dropped). If the window runs past either end of the array, record -1 for position i. Return an array of len(values) such numbers.
Example 1
values=[4, 0, 8, 2, 5], radius=1 → [-1, 4, 3, 5, -1]

Position 1: (4 + 0 + 8) / 3 = 4; position 2: (0 + 8 + 2) / 3 = 3 after dropping the fraction; position 3: (8 + 2 + 5) / 3 = 5. The end positions lack a neighbour, so they get -1.

Example 2
values=[6, 3], radius=0 → [6, 3]

With radius 0 the window is just the element itself.

Example 3
values=[1, 2, 3], radius=2 → [-1, -1, -1]
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁵
  • 0 ≤ radius ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumEvenly Stepped Stretches MediumLongest Run of Ones With One Fix MediumNearest k Values MediumWindows With a Small Product Medium
loading Python…
Input
values=[4, 0, 8, 2, 5], radius=1
Expected
[-1, 4, 3, 5, -1]

Run checks the first 3 tests in your browser. Submit checks all 97 tests on the server, hidden ones included.