Sliding Window Maximum

Program to find Maximum of all subarrays of size K (Sliding Window Maximum) Problem

You are provided with an array of integers, nums, and a positive integer, k. Imagine a sliding window of size k that begins at the far-left side of the array. 

This window moves one position to the right at a time until it fully traverses the array, covering all possible subarrays of length k. 

At each step of this sliding process, you need to identify and record the maximum element within the current window.

Task is to return a list containing these maximum values for each position of the sliding window.

Maximum of All Subarray

Explanation :

Window position         Max
—————                 —–
[1 2 1] 0 4 2 6                 2
1 [2 1 0] 4 2 6                 2
1 2 [1 0 4] 2 6                 4
1 2 1 [0 4 2] 6                 4
1 2 1 0 [4 2 6]                 6

Constraints:

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • 1 <= k <= nums.length

Program to find Maximum of all subarrays of size K (Sliding Window Maximum) Solution

Recommendation for Time and Space Complexity –You should aim for a solution as good or better than O(nlogn) time and O(n) space, where n is the size of the input array.

Hints for solving problems

Hint 1 :

A brute force solution would involve iterating through each window of size k and finding the maximum element within the window by iterating through it. This would be an O(n * k) solution. Can you think of a better way? Maybe think of a data structure that tells the current maximum element of the window in O(1) time.

Hint 2 :

A heap is the best data structure to use when dealing with maximum or minimum values and it takes O(1) time to get the max or min value. Here, we use a max-heap. But what should we do if the current maximum element is no longer part of the window? Can you think of a different way of adding values to the max-heap?

Hint 3 :

We process each window by adding elements to the heap along with their indices to track whether the maximum value is still within the current window. As we move from one window to the next, an element may go out of the window but still remain in the max-heap. Is there a way to handle this situation efficiently?

Hint 4 :

We can ignore those elements that are no longer part of the current window, except when the maximum value is outside the window. In that case, we remove elements from the max-heap until the maximum value belongs to the current window. Why? Because those elements will be eventually removed when the maximum element goes out of the window.

There are mainly 5 approach to solve this problem-

  1. Brute Force Method
  2. Segment Tree Method
  3. Heap Method
  4. Dynamic Programing Method
  5. Deque Method

1. Brute Force Method

Iterate through each window of size k, compute the maximum element in the window, and store it in the result. This method is simple but inefficient with a time complexity of  O(n⋅k).

  • Time complexity: O(n^2)
  • Space complexity: O(1)

where n is the length of the array and k is the size of the window.

Code

2. Segment Tree Method

Construct a segment tree to efficiently find the maximum value in any range. Update the maximum value for each sliding window by querying the tree, achieving O(nlogn) time complexity.

  • Time complexity: O(n logn)
  • Space complexity: O(n)

Code

3. Heap Method

Use a max-heap to track the maximum element in the current window. Remove elements outside the window and push new elements into the heap, maintaining the maximum in O(n logk) time complexity.

  • Time complexity: O(n logn)
  • Space complexity: O(n)

Code

4. Dynamic Programming Method

Precompute maximums for overlapping window segments in two arrays (left-to-right and right-to-left sweeps). Combine these precomputed values to get the maximum for each window in O(n).

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

5. Deque Method

Use a deque to maintain indices of useful elements in the current window. Update the deque as the window slides, ensuring the maximum can be retrieved in O(1). This method runs in O(n) time.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

More Articles