K Radius Subarray Averages LeetCode solution in C++
1class Solution {2public:3vector<int> getAverages(vector<int> &nums, int k) {4int n = nums.size();5int windowSize = 2 * k + 1;67long long windowSum = 0;8vector<int> result(n, -1);910if (n < windowSize) {11return result;12}1314for (int i = 0; i < n; ++i) {15windowSum += nums[i]; // Add nums[i] to the window sum1617if (i - windowSize >= 0) {18windowSum -= nums[i - windowSize]; // Remove nums[i - windowSize] from the window sum19}2021if (i >= windowSize - 1) {22result[i - k] = windowSum / windowSize; // Calculate and store the average in the result23}24}2526return result;27}28};