Skip to content

K Radius Subarray Averages

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

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).

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

Constraints

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