Maximum Average Subarray I
Problem Statement
You are given an integer array nums and a number k.
Look at every contiguous block of exactly k elements and work out its average (sum ÷ k). Return the largest average of any such block.
Answers within 10^-5 of the true value are accepted.
An integer array nums of length n, and an integer k.
A double: the maximum average of any subarray of length exactly k.
The size-4 blocks have sums 2, 51 and 42. The best is [12, -5, -6, 50] with sum 51, so the average is 51 / 4 = 12.75.
There is only one block, [5], and its average is 5.
- • n == nums.length
- • 1 ≤ k ≤ n ≤ 10^5
- • −10^4 ≤ nums[i] ≤ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recompute the sum of every block of size k, keep the best
Slide one running sum; divide once at the end
There are n − k + 1 blocks of size k. Add up each one from scratch, keep the largest sum, and divide it by k at the end.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.