Maximum Sum Subarray of Size K
Keep the sum of the current block in one variable. Moving the block one step right changes exactly two elements: one enters on the right, one leaves on the left. So update the sum with one addition and one subtraction.
The window is valid when it contains exactly k elements. Because we always add one and remove one, it stays valid after the first k elements are loaded — there is no shrinking decision to make.
Problem
You are given an array of integers arr and a number k.
Look at every contiguous block of exactly k elements. Return the largest sum that any such block has.
An integer array arr of length n, and an integer k.
A single number: the maximum sum of any subarray of length exactly k.
The blocks of size 2 are [100,200]=300, [200,300]=500, [300,400]=700. The largest is 700.
The block [4, 2, 10, 23] adds up to 39, which is the largest of all size-4 blocks.
- • 1 ≤ k ≤ n ≤ 10^6
- • −10^6 ≤ arr[i] ≤ 10^6
- • A sum can exceed the int range — use a 64-bit type.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Is the answer about a contiguous block, or can I pick elements from anywhere?
- 2.
Is the block size fixed or does it change?
- 3.
If I already know the sum of one block, how does the sum change when I move the block one step to the right?
- 4.
Which element enters the block, and which element leaves it?
- 5.
How many blocks of size k are there in total?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Recompute the sum of every block of size k | O(N · K) | O(1) |
| Better | Precompute running totals; each block sum is one subtraction | O(N) | O(N) |
| Optimal | Keep one running window sum; add the new element, drop the old one | O(N) | O(1) |
Why does this belong to this pattern?
- “contiguous block”The answer is a range [l … r], not any subset → think window.
- “exactly k elements”The window size never changes → Fixed window, no shrinking logic.
- “largest sum”Aggregate (sum) that can be updated in O(1) when one element enters and one leaves.
This is the anchor problem for the whole pattern: it has a window, but no decision about when to shrink. Master the add-right / remove-left move here; every other window problem adds a validity rule on top of it.
Don't make this mistake
With up to 10^6 elements of size 10^6, a sum can reach 10^12, which overflows a 32-bit int.
If every number is negative, the true answer is negative. Starting at 0 would wrongly return 0.
After adding arr[right], the window must be [right−k+1 … right]. The element that just left is at index right − k. Draw 5 boxes and check it once.
There is no "valid / invalid" condition here. If you find yourself writing a while loop to shrink the window, you are solving a different problem. Fixed size → one add, one remove, every step.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow 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.