Skip to main content
Back to problems
Leetcode
Easy
Arrays
Sliding Window
Math
Maximum Average Subarray I

Find the maximum average value among all contiguous subarrays of length k.

Acceptance 100%
Problem Statement

Problem

Given an integer array nums and an integer k, consider every contiguous subarray of length exactly k. Your task is to compute the largest possible average among them.

Because averages can be fractional, return the answer as a real number.

Clarification

A subarray must consist of consecutive elements, and only subarrays with length exactly k are considered.

Input Format

  • nums: an integer array
  • k: the required subarray length

The exact runtime input format varies by platform; conceptually the function receives these two values.

Output Format

  • Return a floating-point number representing the maximum average over all length-k subarrays.

Constraints

  • 1 <= k <= nums.length
  • nums.length is at least k
  • Values may be positive, negative, or zero
  • Answer is judged with a small floating-point tolerance
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1, 12, -5, -6, 50, 3], k = 4

Output

12.75

Explanation

The length-4 subarrays are [1,12,-5,-6], [12,-5,-6,50], [-5,-6,50,3]. Their averages are 0.5, 12.75, and 10.5 respectively, so the maximum is 12.75.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.