Skip to contentKEEP THE CRAFTTHE SANCTUARY

K-Radius Subarray Averages

For each index, average the k values on each side of it, or return -1 when there are not enough.

The problem

Implement Solution.getAverages(nums, k). For every index i, look at the window from i - k to i + k, which has 2k + 1 values. If that window fits inside nums, the answer at i is the sum of the window divided by 2k + 1, rounded down; otherwise the answer at i is -1. Return the list of answers. For example, nums = [1, 2, 3, 4] with k = 1 gives [-1, 2, 3, -1]. nums has between 1 and 100,000 values, each from 0 to 100,000, and 0 <= k <= 100,000.

Starter code

class Solution:
    def getAverages(self, nums, k):
        pass

Public examples

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

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.