Skip to content
Decoder

K Radius Subarray Averages

Medium

Problem

Given an array nums and a radius k, build an output array of the same length. For each index i, look at the `k` elements on each side of `i` plus nums[i] itself (that is 2k + 1 elements). If that range runs off either end of the array, the output at i is -1. Otherwise it is the average of those elements, rounded down to an integer (integer division).

Example 1

nums = [7, 4, 3, 9, 1, 8, 5, 2, 6], k = 3

Example 2

→ [-1, -1, -1, 5, 4, 4, -1, -1, -1]

Example 3

Input
nums = [8], k = 100000
Output
[-1]

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i], k <= 10^5

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.