Skip to main content
  1. LeetCode/

LeetCode 2090: K Radius Subarray Averages

·2 mins· ·
LeetCode Daily Medium Sliding Window
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-08-02

  • Total time: 00:00.00

Intuition
#

This is a fixed-window sliding window problem. There’s no special technique needed to solve this problem - we simply calculate the window size and slide from left to right. If the window is not large enough, we don’t update the value in the result array. We can initialize the result array with all -1 values.

Approach
#

class Solution:
    def getAverages(self, nums: List[int], k: int) -> List[int]:
        
        window_size = (k * 2) + 1
        n = len(nums)
        res = [-1] * n

        if n < window_size:
            return res

        window_sum = 0
        for i in range(n):
            window_sum += nums[i]

            if i - window_size >= 0:
                window_sum -= nums[i - window_size]
            
            if i >= window_size - 1:
                res[i - k] = window_sum // window_size

        return res

Findings
#

  1. Sliding Window Technique: This problem is a classic application of the sliding window technique, where we maintain a fixed-size window and calculate the average of elements within that window.

  2. Edge Case Handling: The key insight is handling cases where the array length is smaller than the required window size (2k + 1). In such cases, we return an array filled with -1.

  3. Window Size Calculation: The window size is calculated as (k * 2) + 1 because we need k elements on each side of the current element, plus the element itself.

  4. Result Array Positioning: When we have a valid window, we place the average at position i - k in the result array, which corresponds to the center of the current window.

  5. Time Complexity: O(n) where n is the length of the input array, as we only need to traverse the array once.

  6. Space Complexity: O(1) extra space (excluding the result array), as we only use a constant amount of additional variables.

Encountered Problems
#

Related

LeetCode 1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
·3 mins
LeetCode Daily Medium Sliding Window Array Prefix Sum Average Calculation
LeetCode Problem Solving - Fixed Size Sliding Window with Sum Calculation
LeetCode 3: Longest Substring Without Repeating Characters
·2 mins
LeetCode Daily Medium Sliding Window Hashmap
LeetCode Problem Solving
LeetCode 438: Find All Anagrams in a String
·4 mins
LeetCode Daily Medium Sliding Window Hash Table String Anagram Frequency Counting
LeetCode Problem Solving - Sliding Window with Character Frequency
LeetCode 567: Permutation in String
·4 mins
LeetCode Daily Medium Sliding Window String Permutation Frequency Counting Hash Table
LeetCode Problem Solving - Sliding Window with Character Frequency
LeetCode 643: Maximum Average Subarray I
·1 min
LeetCode Daily Easy Sliding Window
LeetCode Problem Solving
LeetCode 713: Subarray Product Less Than K
·3 mins
LeetCode Daily Medium Sliding Window
LeetCode Problem Solving