K Radius Subarray Averages


Identifying Problem Isomorphism
“K Radius Subarray Averages” can be mapped to “Moving Average from Data Stream”.
In the “Moving Average from Data Stream” problem, we are maintaining a moving window of size M and calculating the average of the numbers in this window as new elements get added to the stream. This concept is similar to calculating the kradius average in the “K Radius Subarray Averages” problem, where k denotes the radius around a specific index.
The reason this is an approximate mapping is due to a few differences. In the “K Radius Subarray Averages” problem, we need to manage a varying window size when the index is close to the beginning or end of the array, and assign a value of 1 when there are fewer than k elements on either side. The “Moving Average from Data Stream” problem doesn’t have this requirement, as the window size is constant once it has filled up, and it doesn’t require us to deal with boundary conditions.
“Moving Average from Data Stream” is simpler, as it deals with a constant window size and doesn’t have the boundary condition handling. “K Radius Subarray Averages” problem requires us to handle these additional complexities.
10 Prerequisite LeetCode Problems
The problem can be solved using the sliding window approach, which is a common technique for array problems where a certain condition needs to be satisfied in a subarray or segment of the array. Here are some problems to understand and apply the sliding window technique:
LeetCode 3: Longest Substring Without Repeating Characters Here you need to find the length of the longest substring without repeating characters, which can be solved using the sliding window approach.
LeetCode 209: Minimum Size Subarray Sum The problem is about finding the minimal length of a contiguous subarray of which the sum is greater than or equal to a target number.
LeetCode 567: Permutation in String This problem involves checking if a permutation of the first string exists in the second string, which can be solved using the sliding window technique.
LeetCode 904: Fruit Into Baskets This problem involves finding the maximum number of fruits you can collect, which can be seen as a longest subarray problem that can be solved using the sliding window approach.
LeetCode 1004: Max Consecutive Ones III Here you’re allowed to flip at most K zeroes to ones to find the longest subarray consisting of all ones.
LeetCode 1052: Grumpy Bookstore Owner In this problem, you need to find the maximum number of customers that can be satisfied over a period of minutes, which involves a variant of the sliding window technique.
LeetCode 424: Longest Repeating Character Replacement This problem involves replacing some characters to get the longest possible substring having all repeating letters.
LeetCode 978: Longest Turbulent Subarray This problem involves finding a subarray where the comparison sign flips between each adjacent pair of numbers.
LeetCode 485: Max Consecutive Ones This problem requires you to find the maximum number of consecutive 1s in a binary array, which can be solved with the sliding window technique.
LeetCode 1438: Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit This problem involves finding the longest subarray such that the absolute difference between any two elements is less than or equal to the limit.
These cover various aspects and variations of the sliding window technique which will be beneficial to solve the given problem.


You are given a 0indexed array nums of n integers, and an integer k.
The kradius average for a subarray of nums centered at some index i with the radius k is the average of all elements in nums between the indices i  k and i + k (inclusive). If there are less than k elements before or after the index i, then the kradius average is 1.
Build and return an array avgs of length n where avgs[i] is the kradius average for the subarray centered at index i.
The average of x elements is the sum of the x elements divided by x, using integer division. The integer division truncates toward zero, which means losing its fractional part.
For example, the average of four elements 2, 3, 1, and 5 is (2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75, which truncates to 2.
Example 1:
Input: nums = [7,4,3,9,1,8,5,2,6], k = 3 Output: [1,1,1,5,4,4,1,1,1] Explanation:
 avg[0], avg[1], and avg[2] are 1 because there are less than k elements before each index.
 The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using integer division, avg[3] = 37 / 7 = 5.
 For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.
 For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.
 avg[6], avg[7], and avg[8] are 1 because there are less than k elements after each index. Example 2:
Input: nums = [100000], k = 0 Output: [100000] Explanation:
 The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. Example 3:
Input: nums = [8], k = 100000 Output: [1] Explanation:
 avg[0] is 1 because there are less than k elements before and after index 0.
Constraints:
n == nums.length 1 <= n <= 105 0 <= nums[i], k <= 105
Problem Classification
The “K Radius Subarray Averages” can be classified as a Running Calculation problem.
The problem statement implies a process of continually updating a calculation (in this case, an average) as you move through a sequence of numbers (which could represent anything  it doesn’t necessarily need to be an ‘array’ in the computer science sense). So, it falls into a category where you’re asked to keep a “running” or ongoing calculation.
Language Agnostic Coding Drills
Basic Syntax and Structure: Familiarize yourself with basic coding structures like variable assignments, loops, and conditions. Understand how to define and use a function.
Arrays/Lists: Understand the concept of arrays/lists, how to initialize them, how to iterate over them, and how to access elements by index.
Arithmetic Operations: Get comfortable with basic mathematical operations: addition, subtraction, division, and multiplication.
Understanding Classes and Methods: Learn how to define classes and methods. Get to know how
self
works in Python.Function Arguments: Understand the use of function arguments. Learn the difference between required and optional arguments, and how to set default values.
Sliding Window Technique: This is a common algorithm design technique for arraybased or listbased problems. It maintains a ‘window’ of elements and with each step, the window moves one element ahead (i.e., it ‘slides’).
Complexity Analysis: Understand the time and space complexity of algorithms.
Problemsolving Approach: This code follows the sliding window approach. The problem is to find the average of all contiguous subarrays of size 2*k+1 in it.
Initialize a result list with 1 values.
The window size is 2*k+1, and the variables
left
andright
are used to mark the start and end of the window.curWindowSum
is used to calculate the sum of the elements in the current window.If the current window size is less than
diameter
, the window expands by moving theright
pointer and adding the new element tocurWindowSum
.If the window size is equal to
diameter
, calculate the average of current window and put it into the middle of the window in the result list, then slide the window to the right by moving bothleft
andright
pointers and updatingcurWindowSum
.Repeat step 4 and 5 until
right
reaches the end of the list.Return the result list, which contains the averages for each sliding window of the list.
Targeted Drills in Python
1. Basic Syntax and Structure:
This drill is to practice basic Python syntax and structures, including defining variables, loops, and ifconditions.


2. Arrays/Lists:
Understanding how to create, iterate and access list elements in Python.


3. Arithmetic Operations:
Practice basic arithmetic operations.


4. Understanding Classes and Methods:
Learn to define and use classes and methods.


5. Function Arguments:
Understand function arguments, optional arguments and default values.


6. Sliding Window Technique:
Understand the sliding window technique with a basic example: calculate the moving average of an array.


7. Complexity Analysis:
Understanding the time complexity is not a concept that can be practiced through a coding drill, but rather by analyzing different algorithms and recognizing their time and space complexity. Consider reading about Big O notation and common complexity classes like O(1), O(n), O(n^2), O(log n), O(n log n), etc.