Moving Average
Given an array of length n
and a size k
compute the sum of the average value of all subarrays size k
.
Bonus: Can you figure out a way to only use constant memory?
Input
k
: length of the interval requirednums
: array of numbers
Output
a number representing the sum of all average rounded down to the nearest integer.
Examples
Example 1:
Input:
1k = 3 2nums = [1, 2, 3, 4, 5]
Output: 9
Explanation:
(1 + 2 + 3) / 3 + (2 + 3 + 4) / 3 + (3 + 4 + 5) / 3 = 9
Constraints
1 <= n <= 10000
1 <= nums[i] <= 30000
1 <= k <= n