Practice

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

Inputnums = [1,12,-5,-6,50,3], k = 4Output12.75

Explanation. window [12,-5,-6,50], sum 51

Example 2

Inputnums = [5], k = 1Output5.0

Constraints

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.