57. Sliding Window Maximum

Given an array of integers arr, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

Example 1:

Input: arr = [4, 0, -1, 3, 5, 3, 6, 8], k = 3

Output: [4, 3, 5, 5, 6, 8]

Explanation: 

For each window of size k=3, we find the maximum element in the window and add it to our output array.

Example 2:

Input: arr = [20, 25], k = 2

Output: [25]

Explanation: There’s just one window of size 2 that is possible and the maximum of the two elements is our answer.

Now Your Turn!

Pick the correct output for the given input

Input: arr = [1, 3, -1, -3, 5, 3, 6, 7], k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= arr.length <= 105
  • -104 <= arr[i] <= 104
  • 1 <= k <= arr.length

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
vector<int> maxSlidingWindow(vector<int> &arr, int k) {
}
};
Test Case

Input:

Arr
K