Last Updated: 31 Oct, 2025

K-Radius Subarray Averages

Moderate
Asked in company
Frammer AI

Problem statement

You are given a 0-indexed array of integers nums of length n and an integer k.


The k-radius average for a subarray of nums centered at index i is the average of all elements between indices i - k and i + k (inclusive). The average should be calculated using integer division, which truncates any fractional part.


However, if there are fewer than k elements before or after index i, the k-radius average is defined to be -1.


Your task is to build and return an array avgs of length n, where avgs[i] is the k-radius average for the subarray centered at index i.


Input Format:
The first line contains two space-separated integers, N (the size of the array) and k (the radius).

The second line contains N space-separated integers, representing the elements of the nums array.


Output Format:
Print a single line containing N space-separated integers, representing the avgs array.


Note:
The window size for a valid average calculation is 2 * k + 1. The first possible center for a valid average is at index k, and the last is at index n - 1 - k. Any index outside this range will have an average of -1. A sliding window or prefix sum approach is recommended to calculate the sums efficiently.