← Back to DSA Animator
Maximum Average Subarray I LC #643 Easy Sliding Window
Approach
Fixed Sliding Window
Build first window of size k, then slide: add incoming element, subtract outgoing. Track max sum. O(n) time.
Window Sum
sum: 0 avg: 0 best: —
k
Window [L..R]
Sum
Max Avg
Press Play to start
📊
Maximum Average
Algorithm
1
Build first window: sum of nums[0..k-1]
2
Slide: sum += nums[r] - nums[r-k]
3
maxSum = max(maxSum, sum)
4
Return maxSum / k
Time
O(n)
Space
O(1)
Why It Works

Fixed window — instead of re-summing from scratch each time, slide: add incoming element, subtract outgoing.

This reduces the naïve O(nk) approach to O(n).

Max sum ÷ k = max avg (dividing by a constant k preserves order).