Maximum Average Subarray I

Easy Arrays & Hashing Sliding Window · Fixed Original on LeetCode

Maximum Average Subarray I is a easy arrays & hashing problem solved with the sliding window · fixed pattern. The best approach, optimal (fixed sliding window), runs in O(n) time and O(1) space. Below are 2 approaches in Java, from brute force up.

Problem

Given an integer array nums and an integer k, find the contiguous subarray of length exactly k with the largest average and return that average.

Examples

Example 1

Input
nums = [2, 7, -3, 8, 1], k = 2
Output
4.5
Why
[2,7] has average 4.5; no other window of 2 does better.

Example 2

Input
nums = [-4], k = 1
Output
-4.0

Constraints

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

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Brute forceO(n · k)O(1)
Optimal (fixed sliding window)O(n)O(1)

1Brute force

TimeO(n · k)
SpaceO(1)

Sum every window of size k from scratch.

  1. For each start i, add nums[i..i+k-1] and track the max.
Java
class Solution {
    public double findMaxAverage(int[] nums, int k) {
        int best = Integer.MIN_VALUE;
        for (int i = 0; i + k <= nums.length; i++) {
            int sum = 0;
            for (int j = i; j < i + k; j++) sum += nums[j];
            best = Math.max(best, sum);
        }
        return (double) best / k;
    }
}

2Optimal (fixed sliding window)

TimeO(n)
SpaceO(1)

Sum the first window, then slide: add the element entering on the right and subtract the one leaving on the left. Track the best sum and divide once at the end.

  1. sum = nums[0..k-1], best = sum.
  2. For i from k: sum += nums[i] - nums[i - k]; best = max(best, sum).
  3. Return best / k.
Java
class Solution {
    public double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        for (int i = 0; i < k; i++) sum += nums[i];
        int best = sum;
        for (int i = k; i < nums.length; i++) {
            sum += nums[i] - nums[i - k];
            best = Math.max(best, sum);
        }
        return (double) best / k;
    }
}

Edge cases to test

  • k == n (one window)
  • All negative numbers

Hints

Hint 1

When the window slides by one, only two elements change.

FAQ

What is the best time complexity for Maximum Average Subarray I?

Optimal (fixed sliding window) runs in O(n) time and O(1) extra space.

Which pattern does Maximum Average Subarray I use?

It is a arrays & hashing problem that uses the sliding window · fixed pattern. Other problems with the same pattern: K Radius Subarray Averages, Maximum Number of Vowels in a Substring of Given Length.

Is there a brute force solution for Maximum Average Subarray I?

Yes. Brute force takes O(n · k) time and O(1) space. Sum every window of size k from scratch.

Which edge cases should I test for Maximum Average Subarray I?

k == n (one window); All negative numbers.