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- 01easy
Maximum Average Subarray I
Find the contiguous subarray of a fixed length with the largest average.
- 11easy
Maximum Sum Subarray of Size K
Find the maximum sum of any contiguous subarray of exactly size k.
- 12easy
Contains Duplicate II
Check whether any value repeats inside a distance of at most k indices.
- 13easy
Maximum Number of Vowels in a Substring of Given Length
Find the maximum vowel count inside any substring of a fixed length k.
- 14easy
Number of Sub-arrays of Size K and Average ≥ Threshold
Count fixed-size subarrays whose average is at least a given threshold.
- 45easy
Best Time to Buy and Sell Stock
Find the maximum profit from buying on one day and selling on a later day.
- 46easy
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.
- 47easy
Substrings of Size Three with Distinct Characters
Count substrings of length three whose characters are all different.
- 48easy
Defuse the Bomb
Replace every value in a circular array with the sum of a fixed run of its neighbors.
Medium
17 problems- 02medium
Longest Substring Without Repeating Characters
Find the length of the longest run of characters in a string with no repeats.
- 03medium
Permutation in String
Check whether one string contains a rearrangement of another string as a substring.
- 04medium
Minimum Size Subarray Sum
Find the shortest contiguous subarray whose sum is at least a target value.
- 05medium
Longest Repeating Character Replacement
Find the longest substring you can turn into one repeated letter with a limited number of swaps.
- 06medium
Fruit Into Baskets
Find the longest run of fruit you can collect while carrying only two types at once.
- 07medium
Max Consecutive Ones III
Find the longest run of 1s you can make by flipping a limited number of 0s.
- 15medium
Find All Anagrams in a String
Find every start index where a substring is an anagram of another string.
- 16medium
Grumpy Bookstore Owner
Maximize satisfied customers by calming a grumpy owner for one fixed stretch of minutes.
- 17medium
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.
- 49medium
Binary Subarrays With Sum
Count subarrays of a binary array whose values sum to exactly a target.
- 50medium
Subarray Product Less Than K
Count contiguous subarrays whose product of elements is strictly less than k.
- 51medium
Count Number of Nice Subarrays
Count subarrays that contain exactly k odd numbers.
- 52medium
Longest Subarray of 1s After Deleting One Element
Delete exactly one element and find the longest run of 1s left in the array.
- 53medium
Frequency of the Most Frequent Element
Increase array values up to a total budget to maximize how many end up equal.
- 54medium
Maximum Points You Can Obtain from Cards
Take exactly k cards from either end of a row to maximize their total value.
- 55medium
Get Equal Substrings Within Budget
Find the longest substring you can change into another string within a total cost budget.
- 56medium
Replace the Substring for Balanced String
Find the shortest substring you can replace so every character appears an equal number of times.
Hard
8 problems- 08hard
Minimum Window Substring
Find the shortest substring of one string that contains every character of another string.
- 09hard
Sliding Window Maximum
Find the maximum value inside every fixed-size window as it slides across an array.
- 10hard
Substring with Concatenation of All Words
Find every starting point where a string is formed by joining all given words in any order.
- 18hard
Longest Substring with At Most K Distinct Characters
Find the longest substring that uses at most k distinct characters.
- 19hard
Subarrays with K Different Integers
Count contiguous subarrays that contain exactly k different integers.
- 57hard
Sliding Window Median
Find the median of every fixed-size window as it slides across an array.
- 58hard
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.
- 59hard
Shortest Subarray with Sum at Least K
Find the length of the shortest contiguous subarray whose sum is at least k, with negatives allowed.
Related concepts
| Topic | Description |
|---|---|
| Two Pointers | Scan a sorted array or string from both ends at once to find pairs, remove duplicates, or reverse data in O(n). |
| Arrays | Ordered indexable collection for O(1) access by position. |
| Binary Search | Cut a sorted range in half on every step to find a value or boundary in O(log n) instead of checking one by one. |
| Depth-First Search | Explore as far as possible down one path before backtracking, used to walk trees, graphs, and grids. |
| Breadth-First Search | Explore level by level from a starting point, the go-to way to find the shortest path in an unweighted graph. |
| Dynamic Programming | Break a problem into overlapping subproblems and reuse their answers to avoid recomputing the same work. |