Moving Average
MediumStatisticsSliding WindowArray
Description
Given an array nums and a window size k (1 ≤ k ≤ nums.length), return the array of averages of every contiguous window of size k, each rounded to 4 decimal places.
Examples
Input:
nums = [1,2,3,4,5], k = 2Output:
[1.5,2.5,3.5,4.5]Explanation:
Averaging each window of 2 consecutive values produces [1.5,2.5,3.5,4.5].
Input:
nums = [10,20,30], k = 3Output:
[20]Explanation:
Averaging each window of 3 consecutive values produces [20].
Input:
nums = [4,8,6,2], k = 2Output:
[6,7,4]Explanation:
Averaging each window of 2 consecutive values produces [6,7,4].
Constraints
- •
1 ≤ k ≤ nums.length ≤ 10⁴