Discover how a simple queue trick can speed up finding maximums in moving windows!
Why Sliding Window Maximum Using Deque in DSA C++?
Imagine you have a long list of daily temperatures, and you want to find the hottest day in every 3-day period. Doing this by checking each group of 3 days one by one feels like repeating the same work again and again.
Manually checking each group means looking at many overlapping days multiple times. This wastes time and can cause mistakes, especially if the list is very long. It's slow and tiring to do by hand or with simple code.
Using a deque (a special double-ended queue) helps keep track of the hottest days efficiently. It remembers only the useful days and quickly updates as the window moves, so you don't repeat work and get results fast.
for (int i = 0; i <= n - k; i++) { int max_val = arr[i]; for (int j = i + 1; j < i + k; j++) { if (arr[j] > max_val) max_val = arr[j]; } cout << max_val << " "; }
deque<int> dq; for (int i = 0; i < n; i++) { while (!dq.empty() && arr[dq.back()] <= arr[i]) dq.pop_back(); dq.push_back(i); if (dq.front() <= i - k) dq.pop_front(); if (i >= k - 1) cout << arr[dq.front()] << " "; }
This method lets you find the maximum in every sliding window quickly, even for huge lists, making your programs fast and efficient.
Think of monitoring the highest stock price in the last hour, updating every minute. Using this method, you get the answer instantly without checking all prices every time.
Manual checking repeats work and is slow.
Deque keeps track of useful elements only.
Sliding window maximum becomes fast and easy.
Practice
Solution
Step 1: Understand the role of deque
The deque stores indices of elements in the current window, maintaining them in decreasing order of their values.Step 2: How deque helps find maximum
The front of the deque always holds the index of the maximum element for the current window, allowing O(1) access.Final Answer:
To store indices of elements in a way that helps find the maximum in O(1) time per window -> Option AQuick Check:
Deque stores indices for max retrieval [OK]
- Thinking deque stores actual values instead of indices
- Assuming deque sorts the entire array
- Believing deque reverses the array
Solution
Step 1: Identify deque operation for removing from back
pop_back() removes the last element from the deque, which is used to discard smaller elements behind the current element.Step 2: Why pop_back() is used here
We remove elements from the back that are smaller than the current element to maintain decreasing order in the deque.Final Answer:
pop_back() -> Option CQuick Check:
pop_back() removes smaller elements from back [OK]
- Using pop_front() instead of pop_back() to remove from back
- Confusing push operations with pop operations
- Removing elements from front incorrectly
[2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56] with window size 4?Solution
Step 1: Slide window and find max for each window
Windows and max values: [2,1,3,4] -> 4, [1,3,4,6] -> 6, [3,4,6,3] -> 6, [4,6,3,8] -> 8, [6,3,8,9] -> 9, [3,8,9,10] -> 10, [8,9,10,12] -> 12, [9,10,12,56] -> 56Step 2: Collect all max values in order
The max values for each window are [4, 6, 6, 8, 9, 10, 12, 56]Final Answer:
[4, 6, 6, 8, 9, 10, 12, 56] -> Option DQuick Check:
Sliding window max matches [4, 6, 6, 8, 9, 10, 12, 56] [OK]
- Confusing window size or missing last windows
- Incorrectly calculating max for each window
- Off-by-one errors in output length
for (int i = 0; i < n; i++) {
while (!dq.empty() && arr[dq.back()] <= arr[i]) dq.pop_back();
dq.push_back(i);
if (dq.front() == i - k) dq.pop_front();
if (i >= k - 1) result.push_back(arr[dq.front()]);
}Solution
Step 1: Understand when to remove front index
The front index should be removed if it is out of the current window. The window covers indices [i-k+1, i]. So indices less than i-k+1 are out.Step 2: Correct condition for popping front
The condition should be dq.front() <= i - k to remove indices outside the window.Final Answer:
The condition to pop front should be dq.front() <= i - k -> Option BQuick Check:
Pop front if index outside window [OK]
- Using equality instead of less than for popping front
- Off-by-one errors in window boundary
- Not removing outdated indices from deque
[1, 3, -1, -3, 5, 3, 6, 7] and window size 3, how would you modify the deque approach to also return the minimum in each sliding window along with the maximum?Solution
Step 1: Understand separate tracking for max and min
To get both max and min efficiently, maintain two deques: one keeps indices of max candidates in decreasing order, the other keeps indices of min candidates in increasing order.Step 2: How two deques work together
For each window, the front of max deque gives max, front of min deque gives min. Both deques are updated similarly but with opposite ordering.Final Answer:
Use two deques: one for max (decreasing) and one for min (increasing) values -> Option AQuick Check:
Two deques track max and min separately [OK]
- Trying to store both max and min in one deque
- Sorting each window (inefficient)
- Using wrong data structures for min tracking
Ready to prove it?
Pass the test and this pattern moves unknown
