![]() |
VOOZH | about |
When we are dealing with problems that require checking answers of some ranges in an array, the Sliding window algorithm can be a very powerful technique.
Sliding window problems are computational problems in which a fixed/variable-size window is moved through a data structure, typically an array or string, to efficiently process or analyze the continuous subsets of elements. This technique is used to optimize calculations and find patterns, making it a common approach in algorithm design.
Follow the Sliding Window Technique post to learn more about this algorithm.
- These problems generally evolve around Finding Maximum / Minimum Subarray, Substrings which satisfy some specific condition.
- The size of the subarray or substring 'K' will be given or asked in some of the problems.
- These problem can easily be solved in O(n2) time complexity using nested loops, using sliding window we can solve these in O(n) Time Complexity.
- Required Time Complexity: O(n) or O(nlog(n))
- Constraints: n <= 106 , If n is the size of the Array / String.
In this following article, we’ll explore the different patterns where we can apply the sliding window technique with the help of problems and examples.
There are generally two categories of Sliding window problems:
Type 1: Problems where we are generally given a specific size of window 'K' in the question itself.
For example: Given an array of integers and a number K, find the maximum sum of a subarray of size K.
In this question we already given the size of the subarray we just have to iterate in the array and calculate the sum of each subarray of size k with sliding window technique.
Similar problems following same approach:
Problem | Practice Link |
|---|---|
Sum of minimum and maximum elements of all subarrays of given size K | |
Type 2: Problems in which rather than giving the length question ask about the maximum / minimum fixed length then we can also apply the fixed size sliding window technique.
For example: Maximum subarray size, such that all subarrays of that size have sum less than K.
In this question we have to find the maximum size of the subarray which satisfy the given condition. In these type of questions we can apply Binary Search on Answer + Sliding Windowto solve the question, We can find our possible size of subarray by applying binary search on subarray size and find the condition validation using sliding window of fixed size K , which will be equal to mid value in Binary Search.
Similar problems following same approach:
Problem | Practice Link |
|---|---|
In these sliding window questions we have been asked about the maximum or minimum subarray/substring with some conditions (like having largest sum, smallest sum etc.)
For example: Find length of the longest substring without repeating characters.
To solve the problems based on the above category follow the below intuition steps:
Similar problems following same approach:
Problem | Practice Link |
|---|---|
Find length of the longest substring without repeating characters. | |