Maximum In Sliding Windows Of Size K
Given an array/list of integers of length ‘N’, there is a sliding window of size ‘K’ which moves from the beginning of the array, to the very end. You can only see the ‘K’ numbers in a particular window at a time. For each of the 'N'-'K'+1 different windows thus formed, you are supposed to return the maximum element in each of them, from the given array/list.
Input format :
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.
Output format :
For each test case, print the output array/list which contains the sliding window maximum in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
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.
CodingNinjas
author
2y
Brute Force
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 whi...read more
CodingNinjas
author
2y
Map Based Approach
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 t...read more
CodingNinjas
author
2y
Deque Based Approach
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 maxim...read more
Add answer anonymously...
Top Lifesight Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app