Maximum Average Subarray I

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Maximum Average Subarray I

Approach

Use the same fixed-size sliding window as Maximum Sum Subarray. Build the first window of k elements, then slide across, tracking the maximum sum. At the end, divide the maximum sum by k to get the maximum average.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Sliding Window (Fixed Size)

Why It Works

Since k is constant, the subarray with the maximum sum also has the maximum average. We only need to divide once at the end rather than at each step, keeping the logic simple and efficient.

Updated Feb 2026