Maintain maxDeque (decreasing) and minDeque (increasing). Both store indices. On adding index : pop maxDeque back while . Pop minDeque back while . On shrinking from : if maxDeque front = , pop front. Same for minDeque. Check: . If not, increment and repeat. Both queues shrink from the front when elements leave the window. Track indices, not values.
Time complexity: .
Space complexity: in time and in space.