🪟Sliding Window🪟 Summary

Sliding window technique is useful for solving problems involving subarray or substring.

These problems might involve finding the maximum/minimum sum, count of elements satisfying certain conditions, or finding subarrays with specific properties.

Genrally enumerating all substrings/subarrays take O(n^2) time ,But with sliding window algorithm we can reduce that to O(n) time.

Two types Sliding Window :

  1. Fixed size
  2. Variable size

1.🔥Fixed Size Window

Theory

The length of the window is fixed , generally predefined window size
How to implement the window in code ?
Two pointers : start and end that represent start and end of window
When to think of sliding window FIXED SIZE ?
When we need to process adjacent elements ,something like abs(i - j) <= k. is mentioned in problem.
Or when we need to process k previous/next elements

Note: Purpose of the following template is not to remember it for solving each question , but to get familiar with the topic and once you are confident enough with the pattern you can solve it whichever way you want.

Template
////credits fot template @imanishkumar7545 
fixed_Size_window()
{
    int low = 0, high = 0, windowsize = k;
    while (i < sizeofarray)
    {
        // Step 1: Create a window that is one element smaller than the desired window size
        if (high - low + 1 < windowsize)
        {
            // Generate the window by increasing the high index
            high++;
        }
        // Step 2: Process the window
        else
        {
            // Window size is now equal to the desired window size
            // Step 2a: Calculate the answer based on the elements in the window
            // Step 2b: Remove the oldest element (at low index) from the window for the next window

            // Proceed to the next window by incrementing the low and high indices
        }
    }
}

Basic Slding Window Fixed Size

https://leetcode.com/problems/contains-duplicate-ii (E) Adjacent k Neighbours process
https://leetcode.com/problems/maximum-average-subarray-i/
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/
https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/ (M) Convert to sliding window
https://leetcode.com/problems/defuse-the-bomb/ E
https://leetcode.com/problems/substring-with-concatenation-of-all-words H
https://leetcode.com/problems/repeated-dna-sequences/ Map of strings fixed window [Best approach Bit manipulation] ✏️

Deque

https://leetcode.com/problems/sliding-window-maximum ✏️
First negative integer in every window of size k [G]


2.🔥Variable size

2a. Standard Sliding Window

  • Standard SW uses a variable INTEGER that represents the window

209. Minimum Size Subarray Sum
713. Subarray Product Less Than K
930. Binary Subarrays With Sum Standard SW with atmost concept | One pass SW | Prefix sum approach
1248. Count Number of Nice Subarrays ✏️
1658. Minimum Operations to Reduce X to Zero Reducible to standard SW
1004. Max Consecutive Ones III ✏️
2024. Maximize the Confusion of an Exam ✏️ Two Pass SW | One Pass SW
2962. Count Subarrays Where Max Element Appears at Least K Times | 1. Atleast k = Total subArray - Atmost k-1 |
1208. Get Equal Substrings Within Budget

2b. Hashmap+Sliding Window
This type of problems require a hashmap or a frequency array to represent a Window

3. Longest Substring Without Repeating Characters
159.Longest Substring with At Most Two Distinct Characters [Premium/CN]
340.Longest Substring with At Most K Distinct Characters[Premium/CN]
3090. Maximum Length Substring With Two Occurrences
76. Minimum Window Substring (HARD)
424. Longest Repeating Character Replacement Similar concept as LC 1004
438. Find All Anagrams in a String
567. Permutation in String
904. Fruit Into Baskets
1695. Maximum Erasure Value 🔁
2958. Length of Longest Subarray With at Most K Frequency
992. Subarrays with K Different Integers Atmost concept
395. Longest Substring with At Least K Repeating Characters ⚠️☠️🚨

2c. PrefixSum+HashMap+Sliding Window

https://leetcode.com/problems/maximum-erasure-value/ 🔁

2d. Bit Manipulation

2401. Longest Nice Subarray BITMASKING + SW ✏️

2e. Greedy

1838. Frequency of the Most Frequent Element SW+Greedy|Can also be solved using Bin Search + Preffix sum




extras Rough Work ⋆༺𓆩☠︎︎𓆪༻⋆ Dont look here

Uncategorized
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ H
https://leetcode.com/problems/minimum-size-subarray-in-infinite-array
https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/

Problem where its hard to identify SW is applicable
https://leetcode.com/problems/frequency-of-the-most-frequent-element/

Other Templates posts regarding Sliding Window

https://leetcode.com/problems/minimum-window-substring/solutions/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems/

https://leetcode.com/problems/find-all-anagrams-in-a-string/solutions/92007/Sliding-Window-algorithm-template-to-solve-all-the-Leetcode-substring-search-problem./

Kadane's algorithm and sliding window algorithms are related but not exactly the same. They both deal with contiguous subarrays, but they solve different types of problems and use different strategies.
While Kadane's algorithm can be seen as a specific type of sliding window algorithm tailored for finding the maximum sum subarray, not all sliding window algorithms are implementations of Kadane's algorithm. They serve different problem-solving purposes within the domain of contiguous subarrays.

https://leetcode.com/problems/maximum-length-of-repeated-subarray/ Sliding one array over another | DP

Comments (1)