Sitemap
Aditya Ojha

Aditya Ojha

It's so Good

Sliding Window

3 min readOct 13, 2026

--

Learn Two Pointer Before this…

If Two Pointers are two fingers moving through an array, then:

Sliding Window = two pointers creating a continuous section of the array.

Imagine this,

Sliding Window Problem

Why need sliding window?

Suppose:

arr = [2, 1, 5, 1, 3, 2]
k = 3

We want:

Maximum sum of any 3 consecutive numbers.

If you look at the problem,

[5,1,3] is of size 3 and gives sum as 9 which is greater than any other consecutive numbers whose sum is maximum.

Two Pointer moves both the pointers freely but in sliding window, we maintain a specific size and only find and maintain results from the window at the moment.

But why use Sliding window ?????

We could calculate every group:

2 + 1 + 5 = 8
1 + 5 + 1 = 7
5 + 1 + 3 = 9 ← best
1 + 3 + 2 = 6

In Sliding window:

When we move from:

[2, 1, 5]

to:

[1, 5, 1]

we didn’t need to calculate everything again.

We already know:

2 + 1 + 5 = 8

So:

8
- 2 ← leaving
+ 1 ← entering
----
7

In this example it seems like a very small change, but suppose we are working in Tree or Graph, we do call a recursive function and then sum like… recrFn(2)+recrFn(1)+recrFn(5) = 8

Using sliding window, we cut two recursive call and just need to call, recrFn(1)=1 and just calculate using the leaving Value and entering Value quickly.

In short, this helps avoid repeatative calculation too.

Sliding Window movement,

Start:

[ 2   1   5   1   3   2 ]
L R
└───────┘
window
sum = 8

Slide one step:

[ 2   1   5   1   3   2 ]
L R
└───────┘
sum = 8 - 2 + 1
= 7

Slide again:

[ 2   1   5   1   3   2 ]
L R
└───────┘
sum = 7 - 1 + 3
= 9 🎯

Slide again:

[ 2   1   5   1   3   2 ]
L R
└───────┘
sum = 9 - 5 + 2
= 6

Answer: 9

Get Aditya Ojha’s stories in your inbox

Join Medium for free to get updates from this writer.

In Code,

int windowSum = 0;
int maxSum = 0;

// First window
for (int i = 0; i < k; i++) {
windowSum += arr[i];
}

maxSum = windowSum;

// Slide the window
for (int right = k; right < arr.size(); right++) {

windowSum += arr[right]; // new element enters
windowSum -= arr[right - k]; // old element leaves

maxSum = max(maxSum, windowSum);
}

Why faster??

Calculate every window from scratch

[2 1 5] → 3 operations
[1 5 1] → 3 operations
[5 1 3] → 3 operations
[1 3 2] → 3 operations

≈ O(N × K)

Sliding Window:

Calculate first window → K

Then every next window:
remove 1
add 1

≈ O(N)

Patterns of Sliding Window:

1. Fixed-size window

The window size is given.

Example:

Find the maximum sum of K consecutive elements.

k=3

[ 2  1  5  1  3  2 ]
└─────┘
size 3
└──────┘
size 3
└──────┘
size 3

The window always stays size 3.

2. Variable-size window

This is where Sliding Window becomes really powerful.

Instead of:

“Give me exactly 3 elements.”

we might ask:

“Find the longest subarray whose sum ≤ 7.”

Now the window can grow and shrink.

Example:

arr = [2, 1, 3, 2, 1]

Start:

[ 2 ]
↑
L/R
sum = 2

Expand:

[ 2  1 ]
↑ ↑
L R
sum = 3

Expand:

[ 2  1  3 ]
↑ ↑
sum = 6

Expand:

[ 2  1  3  2 ]
↑ ↑
sum = 8 ❌

Too large!

So we shrink from the left.

[ 2  1  3  2 ]
↑ ↑
L R
remove 2[ 1  3  2 ]
↑ ↑
sum = 6 ✅

Then continue expanding.

This gives us the core variable-window pattern:

right → expand
if condition becomes invalid:
left → shrink

Template

int left = 0;
int sum = 0;

for (int right = 0; right < n; right++) {

// Expand
sum += arr[right];

// Window is invalid → shrink
while (sum > target) {
sum -= arr[left];
left++;
}

// Current window is valid
// [left ... right]
}

Important to Note:

The Rule: If an array contains negative numbers and asks for subarray sum targets, Sliding Window fails. Use Prefix Sum + HashMap instead.

Checkout my github for more Sliding Window details.

--

--