Maximum Average Subarray I
Module 11 · Sliding Window
Problem
Given nums and an integer k, find the contiguous subarray of length
k with the maximum average, and return that average.
Examples
Example 1
Input
nums = [1,12,-5,-6,50,3], k = 4Output12.75Explanation. window [12,-5,-6,50], sum 51
Example 2
Input
nums = [5], k = 1Output5.0Constraints
1 ≤ k ≤ n ≤ 10⁵ · values in ±10⁴.
Attempt it first
This is the fixed-window lesson with a division tacked on at the end. The whole exercise is noticing that maximizing average is the same as maximizing sum (k is fixed, so dividing by it at the end doesn't change which window wins) — then applying the slide template directly.