2Brute2Best
🔥0

Maximum Sum Subarray of Size K

Easy·GeeksforGeeksBasic Anchor
GeeksforGeeks
30-Second Mental ModelFixed Sliding Window
Complexity:O(N · K)→O(N)
Key Insight

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.

Core Validity Invariant

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.

01

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.

Input

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

Output

A single number: the maximum sum of any subarray of length exactly k.

Example 1
Input: arr = [100, 200, 300, 400], k = 2
Output: 700

The blocks of size 2 are [100,200]=300, [200,300]=500, [300,400]=700. The largest is 700.

Example 2
Input: arr = [1, 4, 2, 10, 23, 3, 1, 0, 20], k = 4
Output: 39

The block [4, 2, 10, 23] adds up to 39, which is the largest of all size-4 blocks.

Constraints
  • • 1 ≤ k ≤ n ≤ 10^6
  • • −10^6 ≤ arr[i] ≤ 10^6
  • • A sum can exceed the int range — use a 64-bit type.
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    Is the answer about a contiguous block, or can I pick elements from anywhere?

  2. 2.

    Is the block size fixed or does it change?

  3. 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. 4.

    Which element enters the block, and which element leaves it?

  5. 5.

    How many blocks of size k are there in total?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceRecompute the sum of every block of size kO(N · K)O(1)
BetterPrecompute running totals; each block sum is one subtractionO(N)O(N)
OptimalKeep one running window sum; add the new element, drop the old oneO(N)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “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.
Contiguous range+fixed size k+aggregate that can be updated incrementally→Fixed Sliding Window

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.

10

Don't make this mistake

Using int for the sum
✗ int windowSum = 0;
✓ long long windowSum = 0;

With up to 10^6 elements of size 10^6, a sum can reach 10^12, which overflows a 32-bit int.

Starting best at 0
✗ long long best = 0;
✓ long long best = sum of the first window;

If every number is negative, the true answer is negative. Starting at 0 would wrongly return 0.

Removing the wrong element
✗ windowSum -= arr[right - k + 1];
✓ windowSum -= arr[right - k];

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.

Confusing this with a variable window

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.

11

Want to implement this?

Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
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.