Maximum Average Subarray I

Sliding Window, problem 2 of 7

Maximum Average Subarray I

Easy

LC #643

arrayfixed window

Not attempted yet

Given an integer list nums and an integer k, look at every block of exactly k consecutive numbers. Return the largest average among those blocks, as a float.

Example 1

Input: nums = [1,12,-5,-6,50,3], k = 4
Output: 12.75
[12,-5,-6,50] sums to 51; 51 / 4 = 12.75.

Example 2

Input: nums = [5], k = 1
Output: 5.0

Example 3

Input: nums = [0,4,0,3,2], k = 1
Output: 4.0

Constraints

  • 1 ≤ k ≤ len(nums) ≤ 10^5
  • -10^5 ≤ nums[i] ≤ 10^5
  • Answers within 10^-5 are accepted.

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.