• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
September 24, 2024 |160 Views

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

Description
Discussion

Sliding Window Maximum | Maximum of All Subarrays of Size k

In this guide, we’ll explore the sliding window maximum problem, which involves finding the maximum value in every subarray of size k in a given array. This problem is widely used in scenarios like analyzing time series data, optimizing algorithms, and solving computational geometry problems. By the end of this tutorial, you will understand how to solve this problem efficiently using the sliding window technique.

Problem Definition

Given an array of integers and an integer k, the task is to find the maximum of every subarray of size k in the array.

Example:

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

Here, for every subarray of size k, we compute the maximum:

  • Subarray [1, 3, -1] → Max = 3
  • Subarray [3, -1, -3] → Max = 3
  • Subarray [-1, -3, 5] → Max = 5
  • Subarray [-3, 5, 3] → Max = 5
  • Subarray [5, 3, 6] → Max = 6
  • Subarray [3, 6, 7] → Max = 7

Key Points Covered:

Brute-Force Approach:

  • A simple approach is to iterate over all subarrays of size k and compute the maximum value for each.
  • Drawback: This method has a time complexity of O(n×k)O(n \times k)O(n×k), where n is the size of the array. For large arrays, this approach becomes inefficient.

Optimized Approach Using Deque:

  • The sliding window maximum problem can be solved more efficiently using a deque (double-ended queue). The deque helps maintain the maximum element within the current sliding window in O(n)O(n)O(n) time.
  • A deque is used to store indices of array elements, ensuring that the maximum element is always at the front of the deque, while irrelevant elements (those outside the current window) are removed from the deque.

Steps to Solve Using Deque:

Initialize an Empty Deque:

  • The deque will store indices of the array elements, ensuring that the maximum of each window can be accessed in constant time.

Traverse Through the Array:

  • For each element in the array, perform the following operations:
    • Remove elements from the deque that are no longer within the current window of size k.
    • Remove elements from the deque that are smaller than the current element, as they cannot be the maximum for the current window.
    • Add the index of the current element to the deque.

Track Maximum Elements:

  • After processing each window (i.e., once the size of the deque reaches k), the element at the front of the deque is the maximum of the current subarray.

Move the Window:

  • Slide the window to the right by one position, and repeat the process for the next window of size k.

Example Walkthrough:

Let’s walk through the example arr = [1, 3, -1, -3, 5, 3, 6, 7] with k = 3:

Initial Subarray [1, 3, -1]:

  • Max = 3 (indices stored in deque: [1])

Next Subarray [3, -1, -3]:

  • Max = 3 (deque maintains [1] after adjusting for window size)

Next Subarray [-1, -3, 5]:

  • Max = 5 (deque is updated to store [4] as 5 is greater than the previous elements)

Next Subarray [-3, 5, 3]:

  • Max = 5 (deque still maintains index [4])

Next Subarray [5, 3, 6]:

  • Max = 6 (deque updated to [6] after comparing and removing smaller elements)

Next Subarray [3, 6, 7]:

  • Max = 7 (deque updated to [7])

The result is [3, 3, 5, 5, 6, 7].

Time Complexity:

The optimized solution using a deque has a time complexity of O(n)O(n)O(n), where n is the size of the array. This is because each element is added to and removed from the deque at most once. This solution is much more efficient than the brute-force approach.

Applications of Sliding Window Maximum:

Time Series Data Analysis:

  • Sliding window maximum is useful for analyzing trends in time series data, where the goal is to track the maximum values over a specific window of time.

Signal Processing:

  • In signal processing, the sliding window maximum helps filter noise by keeping track of the highest signal values over a specific interval.

Stock Market Analysis:

  • For analyzing stock prices, sliding window maximum is used to compute the maximum stock price over a given period, such as a week or a month.

Why Learn Sliding Window Maximum?

Understanding the sliding window maximum algorithm is crucial for solving problems that require efficient handling of subarrays or subsequences. Mastering this technique is useful for optimizing solutions in competitive programming, data analysis, and real-time processing of large datasets.

Topics Included:

Brute-Force vs. Optimized Approach: Comparison of brute-force and deque-based solutions for finding maximum values in sliding windows.

Efficient Use of Deque: Step-by-step explanation of how the deque is used to maintain the maximum element in the current window.

Real-World Applications: Practical scenarios where the sliding window maximum technique can be applied, such as time series analysis and stock market trends.

For a detailed guide and further explanations, check out the full article on GeeksforGeeks: https://www.geeksforgeeks.org/sliding-window-maximum-maximum-of-all-subarrays-of-size-k/.