Sliding Window

Solve fixed-window problems: maximum sum of k elements, moving averages, and O(n) updates as the window slides.

Sliding Window Practice Problems

Easy

9 problems
  1. 01

    Maximum Average Subarray I

    Find the contiguous subarray of a fixed length with the largest average.

    easy
  2. 11

    Maximum Sum Subarray of Size K

    Find the maximum sum of any contiguous subarray of exactly size k.

    easy
  3. 12

    Contains Duplicate II

    Check whether any value repeats inside a distance of at most k indices.

    easy
  4. 13

    Maximum Number of Vowels in a Substring of Given Length

    Find the maximum vowel count inside any substring of a fixed length k.

    easy
  5. 14

    Number of Sub-arrays of Size K and Average ≥ Threshold

    Count fixed-size subarrays whose average is at least a given threshold.

    easy
  6. 45

    Best Time to Buy and Sell Stock

    Find the maximum profit from buying on one day and selling on a later day.

    easy
  7. 46

    Minimum Difference Between Highest and Lowest of K Scores

    Pick k scores from a list so the gap between the highest and lowest picked is as small as possible.

    easy
  8. 47

    Substrings of Size Three with Distinct Characters

    Count substrings of length three whose characters are all different.

    easy
  9. 48

    Defuse the Bomb

    Replace every value in a circular array with the sum of a fixed run of its neighbors.

    easy

Medium

17 problems
  1. 02

    Longest Substring Without Repeating Characters

    Find the length of the longest run of characters in a string with no repeats.

    medium
  2. 03

    Permutation in String

    Check whether one string contains a rearrangement of another string as a substring.

    medium
  3. 04

    Minimum Size Subarray Sum

    Find the shortest contiguous subarray whose sum is at least a target value.

    medium
  4. 05

    Longest Repeating Character Replacement

    Find the longest substring you can turn into one repeated letter with a limited number of swaps.

    medium
  5. 06

    Fruit Into Baskets

    Find the longest run of fruit you can collect while carrying only two types at once.

    medium
  6. 07

    Max Consecutive Ones III

    Find the longest run of 1s you can make by flipping a limited number of 0s.

    medium
  7. 15

    Find All Anagrams in a String

    Find every start index where a substring is an anagram of another string.

    medium
  8. 16

    Grumpy Bookstore Owner

    Maximize satisfied customers by calming a grumpy owner for one fixed stretch of minutes.

    medium
  9. 17

    Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

    Find the longest contiguous subarray where any two values differ by at most a limit.

    medium
  10. 49

    Binary Subarrays With Sum

    Count subarrays of a binary array whose values sum to exactly a target.

    medium
  11. 50

    Subarray Product Less Than K

    Count contiguous subarrays whose product of elements is strictly less than k.

    medium
  12. 51

    Count Number of Nice Subarrays

    Count subarrays that contain exactly k odd numbers.

    medium
  13. 52

    Longest Subarray of 1s After Deleting One Element

    Delete exactly one element and find the longest run of 1s left in the array.

    medium
  14. 53

    Frequency of the Most Frequent Element

    Increase array values up to a total budget to maximize how many end up equal.

    medium
  15. 54

    Maximum Points You Can Obtain from Cards

    Take exactly k cards from either end of a row to maximize their total value.

    medium
  16. 55

    Get Equal Substrings Within Budget

    Find the longest substring you can change into another string within a total cost budget.

    medium
  17. 56

    Replace the Substring for Balanced String

    Find the shortest substring you can replace so every character appears an equal number of times.

    medium

Hard

8 problems
  1. 08

    Minimum Window Substring

    Find the shortest substring of one string that contains every character of another string.

    hard
  2. 09

    Sliding Window Maximum

    Find the maximum value inside every fixed-size window as it slides across an array.

    hard
  3. 10

    Substring with Concatenation of All Words

    Find every starting point where a string is formed by joining all given words in any order.

    hard
  4. 18

    Longest Substring with At Most K Distinct Characters

    Find the longest substring that uses at most k distinct characters.

    hard
  5. 19

    Subarrays with K Different Integers

    Count contiguous subarrays that contain exactly k different integers.

    hard
  6. 57

    Sliding Window Median

    Find the median of every fixed-size window as it slides across an array.

    hard
  7. 58

    Minimum Number of K Consecutive Bit Flips

    Find the fewest length-k flips needed to turn every 0 in a binary array into a 1.

    hard
  8. 59

    Shortest Subarray with Sum at Least K

    Find the length of the shortest contiguous subarray whose sum is at least k, with negatives allowed.

    hard