Facebook Pixel

Sliding Window Maximum | Monotonic Stack

We have an array and a sliding window defined by a start index and an end index. The sliding window moves from left of the array to right. There are always k elements in the window. The window moves one position at a time. Find the maximum integer within the window each time it moves.

Input:

arr = [1, 3, 2, 5, 8, 7]
k = 3

Output:

[3, 5, 8, 8]

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro