The first line contains a single integer ‘T’ denoting the number of test cases. The 'T' test cases follow.
The first line of each test case contains two single space-separated integers ‘N’ and ‘K’ denoting the number of elements in the array/list and the size of the window size respectively.
The second line contains ‘N’ single space-separated integers denoting the elements of the array/list.
For each test case, print the output array/list which contains the sliding window maximum in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^4
1 <= K <= N
0 <= ARR[i] <= 10^5
Where, ARR[i] denotes the i-th element in the array/list.
Time Limit: 1 sec.
Our intuition here is to go through each sliding window and keep track of the maximum element in each sliding window. To implement the same we run two nested loops, where the outer loop which will mark the starting point of the subarray of length k, the inner loop will run from the starting INDEX to INDEX + K, K elements from starting index and store the maximum element among these K elements into the answer.
STEPS :
The basic idea of this approach is to use an efficient data structure for querying the maximum element in the window.
We can use an ordered_map in C++ (TreeMap in java) to perform this task.
Consider the following figure to see what happens when we move the window.
We can observe that when we slide our window, which is currently denoting a subarray [0, 2], two things happen :
Now, we just want to get the maximum element in our updated window. The above task can easily be done using the ordered_map efficiently. Therefore we will use an ordered_map for storing the elements of the window. Since insertion, the deletion and query for the maximum element can be done in O(logN) time in the ordered_map. This approach would be more efficient than the previous one.
Consider the steps as follows :
The basic idea of this approach is to use a deque to store the elements of the window. We will maintain a deque that will store elements in non-increasing order such that the maximum element of the window is at the front of the deque. We will store the index of the elements of the given array/list.
STEPS:
Longest Subarray With Zero Sum
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Negative To The End
Find Duplicate in Array