Sliding Window - Introduction
The Sliding Window technique is a powerful algorithmic pattern used to efficiently solve problems that involve finding a subarray or substring satisfying a given condition. Instead of using nested loops (O(n²)), a sliding window maintains a dynamic range over the input and expands or shrinks it as needed, typically achieving O(n) time.
Contents
What is Sliding Window?
Consider having an array or string and being asked a question about a contiguous subarray or substring, for example, the maximum sum of any k
The Sliding Window technique eliminates this redundant work by maintaining a window
defined by two pointers, leftright
There are two main variants:
- Fixed-size window: the window size
is constant. Both pointers move together.k - Dynamic (variable) window: the window size changes based on a condition. The
pointer expands the window; theright pointer shrinks it when the condition is violated.left
Fixed-size Sliding Window
In a fixed-size window, the window always contains exactly k
Example: Maximum sum of k consecutive elements
Output: 9 (subarray [5, 1, 3])
Sliding Window O(n) approach:
public int maxSumFixedWindow(int[] nums, int k) {
int windowSum = 0;
// Compute sum of first window
for (int i = 0; i < k; i++) {
windowSum += nums[i];
}
int maxSum = windowSum;
// Slide the window: add right element, remove left element
for (int i = k; i < nums.length; i++) {
windowSum += nums[i] - nums[i - k];
maxSum = Math.max(maxSum, windowSum);
}
return maxSum;
}
Complexity:
- Time: O(n). Single pass through the array.
- Space: O(1). No extra space needed.
Dynamic (Variable) Sliding Window
In a dynamic window, the window expands by moving rightleft
Output: 2 (subarray [4, 3])
public int minSubArrayLen(int target, int[] nums) {
int left = 0, windowSum = 0;
int minLen = Integer.MAX_VALUE;
for (int right = 0; right < nums.length; right++) {
windowSum += nums[right]; // expand window
// Shrink window from left while condition is satisfied
while (windowSum >= target) {
minLen = Math.min(minLen, right - left + 1);
windowSum -= nums[left];
left++;
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
General template for dynamic sliding window:
int left = 0;
// some window state (e.g. sum, frequency map)
for (int right = 0; right < n; right++) {
// 1. Add element at right to the window state
// 2. Shrink window from left if constraint is violated
while (/* window is invalid */) {
// Remove element at left from window state
left++;
}
// 3. Update answer with current valid window [left, right]
}
When to use Sliding Window
Sliding window is a strong fit when the problem asks about:
- Maximum or minimum of a contiguous subarray or substring of fixed or variable length.
- Longest or shortest substring satisfying a condition (e.g. at most k distinct chars).
- Counting subarrays that meet a criterion (e.g. sum equals k).
- Problems involving contiguous elements, not arbitrary subsets.
Complexity
- Time complexity: O(n). Each element is added to the window once and removed at most once, so the total number of operations is O(2n) = O(n).
- Space complexity: O(1) to O(k). O(1) for simple sum-based windows; O(k) when using a frequency map (e.g. for character-count windows).