Discover how a simple helper can turn a slow, repetitive task into a fast, smooth process!
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 a lot of work.
Checking every group manually means repeating many comparisons again and again. This takes a lot of time and can easily cause mistakes, especially if the list is very long.
Using a special helper called a deque, we can keep track of the hottest days smartly. It remembers only the important days and quickly tells us the hottest day for each group without rechecking everything.
for (int i = 0; i <= n - k; i++) { int max = arr[i]; for (int j = i + 1; j < i + k; j++) { if (arr[j] > max) max = arr[j]; } printf("%d ", max); }
for (int i = 0; i < n; i++) { while (!deque_empty() && arr[deque_back()] <= arr[i]) deque_pop_back(); deque_push_back(i); if (deque_front() <= i - k) deque_pop_front(); if (i >= k - 1) printf("%d ", arr[deque_front()]); }
This method lets us find the maximum in every sliding window quickly and efficiently, even for very large lists.
Think about monitoring the highest stock price every hour during a trading day to make fast decisions without checking all past prices repeatedly.
Manual checking repeats work and is slow.
Deque keeps track of important elements only.
Sliding window maximum becomes fast and easy.
Practice
Solution
Step 1: Understand the role of deque
A deque stores indices of elements in a way that the front always has the index of the maximum element for the current window.Step 2: Why deque helps in O(1) max retrieval
Because the deque maintains elements in decreasing order, the maximum is always at the front, allowing quick access without scanning the window.Final Answer:
To store indices of elements in a way that helps find the maximum in O(1) time per window -> Option DQuick Check:
Deque stores max indices for O(1) max retrieval [OK]
- Thinking deque stores actual values instead of indices
- Using deque to sort elements
- Using deque to store sums
Solution
Step 1: Understand C data structure declaration
C does not have built-in deque type; we use structs or arrays to implement it.Step 2: Identify correct syntax
struct deque dq; // assuming a deque struct is defined shows a struct declaration which is the correct way in C if a deque struct is defined.Final Answer:
struct deque dq; // assuming a deque struct is defined -> Option AQuick Check:
C uses structs for custom data types [OK]
- Using C++ syntax like deque<int>
- Trying to declare deque like an array without size
- Using Java or other language syntax
[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 of size 4
Windows and max values:
Window 1: [2,1,3,4] max=4
Window 2: [1,3,4,6] max=6
Window 3: [3,4,6,3] max=6
Window 4: [4,6,3,8] max=8
Window 5: [6,3,8,9] max=9
Window 6: [3,8,9,10] max=10
Window 7: [8,9,10,12] max=12
Window 8: [9,10,12,56] max=56Step 2: Collect all max values in order
The max values are [4, 6, 6, 8, 9, 10, 12, 56]Final Answer:
[4, 6, 6, 8, 9, 10, 12, 56] -> Option CQuick Check:
Max per window = [4,6,6,8,9,10,12,56] [OK]
- Including partial windows smaller than size 4
- Off-by-one errors in window indexing
- Confusing max values with sums
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)
printf("%d ", arr[dq.front()]);
}Solution
Step 1: Analyze deque maintenance logic
The code pops smaller elements from back before pushing current index, ensuring decreasing order.Step 2: Check window boundary condition
The code pops front if the front index is out of current window (<= i-k), which is correct.Step 3: Confirm output printing
Printing starts after first full window (i >= k-1), printing max at front index.Final Answer:
No bug, code is correct -> Option BQuick Check:
Deque logic and conditions are correct [OK]
- Removing front before pushing current index
- Using wrong comparison operators in conditions
- Printing output before full window
Solution
Step 1: Understand max vs min logic in deque
For max, we pop from back while elements are smaller or equal. For min, we pop while elements are greater or equal.Step 2: Modify comparison operator
Change condition from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] to maintain increasing order for min.Final Answer:
Change the comparison from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] when popping from back -> Option AQuick Check:
Reverse comparison for min sliding window [OK]
- Reversing array instead of changing comparison
- Using stack which doesn't maintain order
- Changing window size unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
