Skip to content

Maximum Average Subarray I

EasyPython 3Sliding windowArrays

You are given a list of integers nums and an integer k. Find the contiguous subarray of length exactly k with the largest average, and return that average as a float.

Constraints:
  - 1 <= k <= len(nums) <= 10^5
  - -10^4 <= nums[i] <= 10^4
  - answers within 10^-5 of the true value are accepted
Examples:
  [1, 12, -5, -6, 50, 3], k = 4  -> 12.75   (the window [12, -5, -6, 50])
  [5], k = 1                     -> 5.0
  [0, 4, 0, 3, 2], k = 1         -> 4.0
  [-1, -2, -3], k = 2            -> -1.5    (the window [-1, -2])

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…