Maximum Average Subarray I
Recognize the pattern
Brute force idea
If you approach Maximum Average Subarray I in the most literal way possible, you get this: Calculate sum for every subarray of size k. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
Better approach
The real unlock in Maximum Average Subarray I comes when you notice this: Slide a window of size k: add the new element, subtract the old one. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
Key invariant
At the center of Maximum Average Subarray I is one steady idea: The window sum is always the sum of exactly k consecutive elements. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.
Watch out for
The trap in Maximum Average Subarray I usually looks like this: Off-by-one when initializing the first window — sum exactly k elements first. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.