Skip to content
Decoder

Maximum Average Subarray I

Easy

Problem

You get an integer array nums and a length k. Look at every contiguous subarray of length exactly `k` and return the highest average among them.

Example 1

Input
nums = [1, 12, -5, -6, 50, 3], k = 4
Output
12.75
Why
(12 - 5 - 6 + 50) / 4

Example 2

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

Answers within 10^-5 of the true value are accepted.

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.