๐ช Sliding Window Technique
You need to find the maximum sum of 3 consecutive elements in an array. The naive approach recalculates the sum for each window - that's wasteful! Sliding window reuses the previous sum by subtracting the left element and adding the right. Let's see how.
The Problem: Maximum Sum Subarray
Given an array, find the maximum sum of k consecutive elements.
Example
Array: [2, 1, 5, 1, 3, 2]
k = 3
Windows:
[2, 1, 5] = 8
[1, 5, 1] = 7
[5, 1, 3] = 9 โ Maximum!
[1, 3, 2] = 6
Naive: Recalculate each sum โ O(nรk)
Sliding window: Reuse previous sum โ O(n)
How Sliding Window Works
Instead of recalculating the entire sum, slide the window by removing the leftmost element and adding the next element.
The Algorithm (Fixed Window)
- Calculate sum of first
kelements - For each new element:
- Subtract the element leaving the window
- Add the element entering the window
- Track the maximum sum
window_sum = window_sum - arr[i-k] + arr[i]
Click Run to execute your code
- Naive: Recalculate k elements for each window โ O(nรk)
- Sliding: Just 2 operations per window โ O(n)
- For k=100, n=1000: 100,000 ops vs 2,000 ops!
Quick Quiz
-
Why is it called "sliding" window?
Show answer
Because the window "slides" one position at a time, removing the left element and adding the right element - like sliding a physical window frame! -
What's the space complexity?
Show answer
O(1) - we only store the current window sum and max, regardless of array size!
Two Types of Windows
Click Run to execute your code
| Type | Window Size | When to Use | Example |
|---|---|---|---|
| Fixed Window | Constant (k) | Find optimal k-sized subarray | Max sum of k elements |
| Variable Window | Grows/shrinks | Find optimal size based on condition | Longest substring with k distinct chars |
When to Use Sliding Window
โ Use Sliding Window When:
- Finding optimal contiguous subarray/substring
- Problem asks for "maximum/minimum of k elements"
- Problem asks for "longest/shortest substring with condition"
- Elements must be consecutive (no gaps)
โ Don't Use When:
- Elements don't need to be contiguous
- Need to find pairs/triplets (use two pointers)
- Need all subarrays (not just optimal one)
Common Mistakes
โ Forgetting to Initialize Window
# Wrong: Starting from index k
for i in range(k, len(arr)):
window_sum = window_sum - arr[i-k] + arr[i]
# window_sum is undefined!
# Right: Calculate first window
window_sum = sum(arr[:k])
for i in range(k, len(arr)):
window_sum = window_sum - arr[i-k] + arr[i]
โ Wrong Index for Removal
# Wrong
window_sum = window_sum - arr[i] + arr[i+k]
# Right: Remove element k positions back
window_sum = window_sum - arr[i-k] + arr[i]
Summary
The 3-Point Sliding Window Guide
- Pattern: Reuse previous calculation by sliding the window
- Complexity: O(n) time, O(1) space - much better than O(nรk)
- Types: Fixed size (constant k) or variable size (dynamic)
Interview tip: When you see "contiguous subarray" or "substring", think sliding window!
What's Next?
You've mastered array techniques! Next: String Manipulation - learn string operations, pattern matching, and how to apply sliding window to string problems.
Enjoying these tutorials?