Sliding Window Maximum Using Deque in DSA C - Time & Space Complexity
We want to understand how the time needed to find the maximum in each sliding window changes as the input size grows.
How does the algorithm handle many elements efficiently?
Analyze the time complexity of the following code snippet.
void slidingWindowMax(int arr[], int n, int k) {
int deque[n];
int front = 0, back = -1;
for (int i = 0; i < n; i++) {
while (front <= back && arr[deque[back]] < arr[i]) back--;
deque[++back] = i;
if (deque[front] == i - k) front++;
if (i >= k - 1) printf("%d ", arr[deque[front]]);
}
}
This code finds the maximum value in every window of size k in the array using a deque.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop runs through all elements once.
- How many times: Each element is added and removed from the deque at most once.
As the input size grows, each element is processed a limited number of times, so the total work grows roughly in direct proportion to the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The operations grow linearly with input size.
Time Complexity: O(n)
This means the time to complete grows directly in proportion to the number of elements.
[X] Wrong: "Since there is a nested while inside the for loop, the time complexity must be O(n²)."
[OK] Correct: Each element is pushed and popped from the deque only once, so the inner loop does not run n times for each element.
Understanding this efficient approach shows you can handle problems that need fast sliding window calculations, a common real-world need.
"What if we used a simple array instead of a deque to track maximums? How would the time complexity change?"
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
