Step 1: Process first element 2, add its index 0 to deque
Deque: [0]
Window: [2] (indices 0 to 0)
Why: Start building deque with first element index
Step 2: Process element 1 at index 1, 1 < 2 so add index 1 to deque end
Deque: [0, 1]
Window: [2, 1] (indices 0 to 1)
Why: Keep indices of elements in decreasing order of values
Step 3: Process element 3 at index 2, remove indices with smaller values (1 and 0 < 3), remove index 1 and 0, add index 2
Deque: [2]
Window: [2, 1, 3] (indices 0 to 2)
Why: Remove smaller elements as 3 is bigger and will be max in window
Step 4: Process element 4 at index 3, remove index 2 (3 < 4), add index 3
Deque: [3]
Window: [2, 1, 3, 4] (indices 0 to 3)
Why: Keep only indices of elements that can be max in current window
Step 5: Window reached size 4, max is element at deque front index 3 (value 4), slide window forward
Max for window [2,1,3,4] is 4
Deque: [3]
Why: Output max for first window
Step 6: Slide window: remove out-of-window indices from front if any (none), process element 6 at index 4, remove index 3 (4 < 6), add index 4
Deque: [4]
Window: [1, 3, 4, 6] (indices 1 to 4)
Why: Maintain deque for new window
Step 7: Max for window [1,3,4,6] is element at index 4 (6), slide window
Max: 6
Deque: [4]
Why: Output max for second window
Step 8: Process element 3 at index 5, 3 < 6, add index 5
Deque: [4, 5]
Window: [3, 4, 6, 3] (indices 2 to 5)
Why: Add smaller element index to deque end
Step 9: Max for window [3,4,6,3] is element at index 4 (6), slide window
Max: 6
Deque: [4, 5]
Why: Output max for third window
Step 10: Slide window: remove out-of-window indices from front if any (none), process element 8 at index 6, remove indices 5 and 4 (3 and 6 < 8), add index 6
Deque: [6]
Window: [4, 6, 3, 8] (indices 3 to 6)
Why: Remove smaller elements, add new bigger element
Step 11: Max for window [4,6,3,8] is element at index 6 (8), slide window
Max: 8
Deque: [6]
Why: Output max for fourth window
Step 12: Process element 9 at index 7, remove index 6 (8 < 9), add index 7
Deque: [7]
Window: [6, 3, 8, 9] (indices 4 to 7)
Why: Keep max element index at front
Step 13: Max for window [6,3,8,9] is element at index 7 (9), slide window
Max: 9
Deque: [7]
Why: Output max for fifth window
Step 14: Process element 10 at index 8, remove index 7 (9 < 10), add index 8
Deque: [8]
Window: [3, 8, 9, 10] (indices 5 to 8)
Why: Update deque with new max candidate
Step 15: Max for window [3,8,9,10] is element at index 8 (10), slide window
Max: 10
Deque: [8]
Why: Output max for sixth window
Step 16: Process element 12 at index 9, remove index 8 (10 < 12), add index 9
Deque: [9]
Window: [8, 9, 10, 12] (indices 6 to 9)
Why: Keep max element index updated
Step 17: Max for window [8,9,10,12] is element at index 9 (12), slide window
Max: 12
Deque: [9]
Why: Output max for seventh window
Step 18: Process element 56 at index 10, remove index 9 (12 < 56), add index 10
Deque: [10]
Window: [9, 10, 12, 56] (indices 7 to 10)
Why: Update deque with new max
Step 19: Max for window [9,10,12,56] is element at index 10 (56)
Max: 56
Deque: [10]
Why: Output max for last window
Result: Max values for each window: 4 -> 6 -> 6 -> 8 -> 9 -> 10 -> 12 -> 56