No history yet

Introduction to Sliding Window

The Sliding Window Idea

Imagine you're on a moving train, looking out at a long line of colored freight cars. Your view is limited to a single window, so you can only see a few cars at once. As the train moves, your view slides along the line of cars. This is the core concept behind the sliding window technique.

In algorithms, a "window" is simply a contiguous part of a larger data set, like a sub-array or a sub-string. The sliding window pattern involves creating a window over the data and moving it—either by expanding, shrinking, or shifting it—to solve a problem efficiently.

Why Not Just Brute Force?

Let's say we have an array of numbers and we need to find the maximum sum of any contiguous sub-array of size 3.

[1, 8, 3, 5, 2, 9]

A brute-force approach would check every possible sub-array of size 3:

  1. 1 + 8 + 3 = 12
  2. 8 + 3 + 5 = 16
  3. 3 + 5 + 2 = 10
  4. 5 + 2 + 9 = 16

This works, but notice how much work is repeated. To get from the first sum to the second, we recalculated the sum of 8 and 3. This inefficiency gets much worse as the array and sub-array sizes grow.

The sliding window method is smarter. We start with the first window, [1, 8, 3], which sums to 12. To move the window one step, we subtract the number that's leaving (1) and add the number that's entering (5).

12 - 1 + 5 = 16

We've found the sum of the next window, [8, 3, 5], with just two operations instead of summing all three numbers again. This reuse of previous calculations is what makes the sliding window so powerful.

The main advantage of the sliding window is that it avoids redundant work. By intelligently adding and removing elements at the edges of the window, it often reduces a problem's complexity significantly.

When to Use It

The sliding window pattern isn't a silver bullet, but it's incredibly effective for a specific class of problems. You should think about using it whenever you're asked to find an optimal value (like a maximum, minimum, or longest sequence) within a contiguous sub-array or sub-string.

Sliding Window is a technique for problems involving contiguous subarrays or substrings.

Common scenarios include:

  • Finding the maximum or minimum sum of a sub-array of a fixed size.
  • Finding the longest sub-string that has no repeating characters.
  • Finding the smallest sub-array whose sum is greater than or equal to a certain value.

In all these cases, we're dealing with a contiguous block of data and looking for an optimal property within it. That's the signal to consider a sliding window.

Let's test your understanding of these core ideas.

Quiz Questions 1/4

What is the primary advantage of the sliding window technique compared to a brute-force approach for problems involving contiguous subarrays?

Quiz Questions 2/4

You have an array [2, 1, 5, 4, 3, 6] and a window of size 3. The current window is [1, 5, 4], and its sum is 10. What is the sum of the next window?

By recognizing these patterns, you can apply this efficient technique instead of relying on slower, brute-force methods.