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 required
  • nums: 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

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro