Sliding Window Maximum (Maximum of all Subarrays of size K)

Traditionally to find the maximum element in an array, we used a loop that iterates over all the elements and returns us the value. Pseudocode implementation is given below.

Pseudocode

The sliding window maximum technique is a way to find the maximum element in each window of a given size in an array. This technique is useful in many applications, such as when processing streaming data, where you need to find the maximum element in a fixed-sized window of elements at each step.

To implement the sliding window maximum technique, you can use a nested loop approach, where the outer loop iterates over the windows in the array, and the inner loop finds the maximum element in each window. Alternatively, you can use a data structure such as a heap to keep track of the maximum element in each window, reducing the algorithm's time complexity.

In both cases, the sliding window maximum technique has a time complexity of O(n) or O(nlogk), depending on the implementation.

C Code

Output

3 4 5 
Maximum element in the array: 5

Explanation

In this implementation, the find_sliding_window_maximum function takes in an array, arr, its size, n, and the size of the window, k. It then loops through the array and finds the maximum element in each window of size k. Finally, it prints the maximum element in each window and returns the maximum element in the whole array.

Note that this implementation has a time complexity of O(nk) because it uses nested loops to find the maximum element in each window. This can be improved by using a data structure such as a heap to keep track of the maximum element in each window, reducing the time complexity to O(nlogk).

C++ Code

Output

3 3 5 6 7

Explanation:

This implementation uses a double-ended queue (deque) to store the indices of the array elements. The deque always maintains the following invariants:

The elements in the deque are in non-decreasing order, with the largest element at the front and the smallest element at the back.The elements in the deque are within the current window.

These invariants allow us to efficiently find the maximum of each subarray in linear time. The time complexity of this implementation is O(n).

Java Code

Output

3 3 5 5 6 7

The pseudocode in Java (using stack approach)

Python code

Output

3
3
4
5
5
5
6

C# Code

Output

33
4
5
5
5
6

JavaScript Code

Output

3
3
4
5
5
5
6

Why is the Sliding Window Technique better than other Methods for finding the Maximum Element?

The sliding window maximum technique is a useful algorithm for finding the maximum element in each window of a given size in an array. It is often used in applications where you must process streaming data and find the maximum element in a fixed-sized window at each step.

One reason why the sliding window maximum technique may be better than other methods for finding the maximum element is that it can be implemented using a simple nested loop approach, which has a time complexity of O(nk), where n is the size of the array and k is the size of the window. This is relatively efficient compared to other methods, such as sorting the array and finding the maximum element, which has a time complexity of O(nlogn).

Another reason why the sliding window maximum technique may be better is that it can be easily modified to use a data structure such as a heap to keep track of the maximum element in each window, which can reduce the time complexity to O(nlogk). This can be more efficient than other methods for finding the maximum element in a large array, especially if the size of the window is much smaller than the size of the array.

Overall, the sliding window maximum technique is a useful and efficient algorithm for finding the maximum element in each window of a given size in an array.






Latest Courses