LogIn
I don't have account.

Mastering the Sliding Window: Top 50 Problems Tiered & Explained

Raj Verma

18 Views

Sliding window is a powerful technique for optimizing contiguous subarray or substring challenges. This guide presents 50 curated problems—built from trusted sources each with a descriptive summary to highlight what’s being asked and how to think about it.

Easy (Low Complexity) – Foundation Building

  1. Maximum Sum Subarray of Size K
    Compute the max sum over all subarrays of size k.

  2. Smallest Window Containing 0, 1, and 2
    Find shortest subarray that includes all three specified numbers.

  3. Check if Permutation of Pattern Exists as Substring
    Determine whether any substring is a permutation of a target pattern.

  4. Count Strictly Increasing Subarrays
    Count all contiguous subarrays where elements strictly increase.

  5. Remove Consecutive Duplicates
    Collapse consecutive duplicates in a string.

  6. Max Sum Subarray ≤ X
    Find subarray with max sum that does not exceed X.

Medium (Balanced Complexity) – Broadening Your Window Skills

  1. Longest Substring Without Repeating Characters
    Length of the longest substring where all characters are distinct.

  2. Substrings with K Distinct Characters
    Count substrings containing exactly K unique characters.

  3. Fruit-Into-Baskets (Max of Two Types)
    Longest subarray containing at most two distinct types.

  4. Substrings of Length K with k-1 Distinct Elements
    Count substrings that are missing just one distinct value.

  5. Minimum Removals to Meet Target Sum
    Remove as few elements as possible to ensure sum ≤ target.

  6. Longest Repeating Character Replacement
    Maximize a repeated character substring by replacing up to k.

  7. Binary Subarray with Sum = K
    Find subarray in binary array whose sum equals K.

  8. Subarrays Product Less Than K
    Count contiguous subarrays with product < k.

  9. Count Anagrams in String
    Total substrings that are anagrams of a given pattern.

  10. Max Sum Subarray ≥ K
    Maximize sum with a constraint on minimum length.

  11. Count Distinct in Every Window of Size K
    For each window, count unique elements.

  12. Subarray With Given Sum (Positive Ints)
    Locate exact sum in positive-integer arrays.

  13. First Negative in Every Window Size K
    For each window, report the first negative value.

  14. Longest Subarray Sum = K
    Longest contiguous subarray summing exactly to K.

  15. Smallest Window Containing Characters from Another String
    Shortest substring containing all target string characters.

  16. Count Equivalent Subarrays
    Count windows that share the same multiset or signature.

Hard (Advanced Patterns) – Peak Sliding Window Challenges

  1. Maximum of Minimums for Every Window Size
    For each window size, find the maximum among all window minimums.

  2. Longest Substring with Exactly K Unique Characters
    Length of longest substring with exactly K distinct chars.

  3. Minimum Window Substring
    Identify the shortest substring covering all characters of another string.

  4. Largest Sum Subarray ≥ K
    Maximize sum while ensuring window length ≥ k.

  5. Sliding Window Maximum
    For each size-k window, find the maximum.

  6. Permutation in String
    Check if s2 contains a permutation of s1.

  7. Max Consecutive Ones III
    With up to k flips, compute the longest segment of 1s.

  8. Minimum Operations to Reduce X to Zero
    Remove elements from either end to bring sum to X optimally.

  9. Count Subarrays with Exactly K Distinct Integers
    Calculate subarrays with exactly K different values.

  10. Replace Substring to Balance
    Minimal window size to replace so the string becomes balanced (e.g., “QWER”).

  11. Longest Continuous Subarray with |max – min| ≤ Limit
    Find longest window with value differences within limit.

  12. Minimum Consecutive Cards to Pick Up
    Shortest subarray with duplicate values.

  13. Count Nice Subarrays (K Odd Numbers)
    Count subarrays containing exactly k odd numbers.

  14. Get Equal Substrings Within Budget
    Count substrings within cost constraints to match target.

  15. Longest Nice Subarray
    Longest subarray with bitwise AND of all pairs = 0.

  16. Grumpy Bookstore Owner
    Maximize satisfied customers by applying a special technique on a window.

  17. Minimum Swaps to Group All 1s Together II
    Circular array: minimize swaps to group all ones.

  18. Maximize Exam Confusion
    Flip ≤ k answers to maximize same-character substring.

  19. Frequency of Most Frequent Element
    Longest subarray where value + k allows maximum repetition.

  20. Count Subarrays Where Max Appears ≥ K Times
    Count windows where a specific element appears frequently.

  21. Count Continuous Subarrays |max – min| ≤ Threshold
    Count subarrays where the range of values stays bounded.

44–50: Additional Challenging Sliding Window Variants (New Additions)

  1. Fixed-Length Window via Binary Search on Length
    Binary search on window size + sliding window check for constraint validity.

  2. Longest Subarray Where Sum of Elements is Divisible by K
    Sliding window + modular arithmetic + hashing.

  3. Maximum MEX (Minimum Excludant) in All Subarrays of Size K
    Compute MEX (smallest missing non-negative integer).

  4. Dynamic Window Searching for Max Toys Within Budget
    Sliding window + prefix sums + binary search to compute max number of toys purchasable.

  5. Longest K-Unique-Character Substring (Hard Variant)
    Longest substring with at most k unique characters.

  6. Subarrays with Sum = K in Binary Array
    Sliding window optimized for binary arrays.

  7. Combine Fixed & Variable Sliding Window via Two-pointer + Hash
    Solve hybrid constraint where window must satisfy both fixed-length and content-based constraints.

Why This Tiered, Detailed Guide Works

  • Progressive Learning:
    Start with problem basics and advance to nuanced, high-constraint variants.

  • Clarity in Description:
    Each problem summary reveals exact challenge and hinted technique, enabling quick pattern recognition.

  • Source-Validated:
    Problems and variants drawn from GFG and LeetCode community collections, including combination approaches like sliding window + binary search, MEX, and modular arithmetic.

Recommended Strategy to Prepare

  • Start with Easy problems – build pattern fluency.
  • Tackle Medium problems – refine adaptive thinking and constraint tracking.
  • Challenge yourself with Hard-level + variants (44–50) – implement and time-box solutions. Track progress:
    Note Problem | Tier | Solved? | Key Insight in a spreadsheet.

=> Revisit with spaced practice – repetition is key.

Responses (0)

Write a response

CommentHide Comments

No Comments yet.