2Brute2Best
🔥0

Maximum Average Subarray I

Easy·LeetCode #643 Pro OnlyBasic Reinforcement
LeetCode
01

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.

Input

An integer array nums of length n, and an integer k.

Output

A double: the maximum average of any subarray of length exactly k.

Example 1
Input: nums = [1, 12, -5, -6, 50, 3], k = 4
Output: 12.75

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.

Example 2
Input: nums = [5], k = 1
Output: 5.0

There is only one block, [5], and its average is 5.

Constraints
  • • n == nums.length
  • • 1 ≤ k ≤ n ≤ 10^5
  • • −10^4 ≤ nums[i] ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“contiguous subarray”
→ The answer is a range [l … r] → think window.
“length equal to k”
→ The size never changes → Fixed window, no shrinking.
“maximum average”
→ Average with a fixed size k is just sum ÷ k, so maximise the sum.
03

Approach Complexity Ladder

Brute Force — sum every blockO(N · K)O(1)

Recompute the sum of every block of size k, keep the best

Optimal — fixed sliding window on the sumO(N)O(1)

Slide one running sum; divide once at the end

Brute Force Intuition

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:

Window size is fixed at K. Each slide adds one element on the right and removes one on the left — update the aggregate in O(1).
Remember this · Sliding Window
Trigger
Contiguous subarray / substring + longest, shortest, maximum, count, or "of size K".
Think
Can I keep a valid range and move it, instead of checking every subarray from scratch?
Key question
When is my current window valid?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Sliding Window translate into production architectures and low-level memory mechanics.