# 20.06.2023 [2090. K Radius Subarray Averages]
Array containing sliding window of size `2k+1` average or `-1`
20.06.2023
2090. K Radius Subarray Averages medium
blog post
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/251
Problem TLDR
Array containing sliding window of size 2k+1
average or -1
Intuition
Just do what is asked
Approach
careful with
Int
overflow
Complexity
Time complexity:
O(n)Space complexity:
O(n)
Code
fun getAverages(nums: IntArray, k: Int): IntArray {
if (k == 0) return nums
var sum = 0L
val res = IntArray(nums.size) { -1 }
for (i in 0 until nums.size) {
sum += nums[i]
if (i > 2 * k) sum -= nums[i - 2 * k - 1]
if (i >= 2 * k) res[i - k] = (sum / (2 * k + 1)).toInt()
}
return res
}