12. K Sized Subarray Maximum
β GFG solution to the K Sized Subarray Maximum problem: find maximum value for each contiguous subarray of size k using deque-based sliding window technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[] of positive integers and an integer k. Your task is to find the maximum value for each contiguous subarray of size k. Return an array of maximum values corresponding to each contiguous subarray.
A subarray is a contiguous sequence of elements within an array. The goal is to efficiently compute the maximum element in every window of size k as it slides through the array.
π Examples
Example 1
Input: arr[] = [1, 2, 3, 1, 4, 5, 2, 3, 6], k = 3
Output: [3, 3, 4, 5, 5, 5, 6]
Explanation:
1st contiguous subarray [1, 2, 3], max = 3
2nd contiguous subarray [2, 3, 1], max = 3
3rd contiguous subarray [3, 1, 4], max = 4
4th contiguous subarray [1, 4, 5], max = 5
5th contiguous subarray [4, 5, 2], max = 5
6th contiguous subarray [5, 2, 3], max = 5
7th contiguous subarray [2, 3, 6], max = 6Example 2
π Constraints
$1 \le \text{arr.size()} \le 10^6$
$1 \le k \le \text{arr.size()}$
$0 \le \text{arr}[i] \le 10^9$
β
My Approach
The optimal approach uses a Deque-based Sliding Window technique to maintain indices of potentially maximum elements in the current window:
Monotonic Deque Sliding Window
Initialize Variables:
Use a deque to store indices of array elements.
The deque maintains elements in decreasing order of their values.
Result vector to store maximum of each window.
Process Each Element:
Remove indices from the front if they are outside the current window (index <= i - k).
Remove indices from the back if their corresponding values are smaller than or equal to the current element.
Add the current index to the deque.
Extract Maximum:
Once we've processed at least k elements (i >= k - 1), the front of the deque contains the index of the maximum element in the current window.
Add this maximum value to the result.
Maintain Monotonic Property:
The deque always maintains indices in decreasing order of their values.
This ensures the front always has the maximum element's index.
Continue Until End:
Repeat for all elements to get all window maximums.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as each element is added to and removed from the deque at most once. Although we have nested loops, each element is processed a constant number of times across all iterations.
Expected Auxiliary Space Complexity: O(k), as the deque stores at most k indices corresponding to the current window size. The result array requires O(n-k+1) space but that's part of the output, not auxiliary space.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated