Sliding Window Maximum Problem Statement
You are given an array/list of integers with length 'N'. A sliding window of size 'K' moves from the start to the end of the array. For each of the 'N'-'K'+1 possible windows, return the maximum element.
Input:
First line: Single integer ‘T’ indicating number of test cases.
Each test case consists of:
- First line: Two space-separated integers ‘N’ and ‘K’ (number of elements and window size).
- Second line: ‘N’ space-separated integers (array elements).
Output:
For each test case, return the output array containing the maximum elements of each sliding window, on separate lines.
Example:
Input:
T = 1
N = 8, K = 3
Array = [1, 3, -1, -3, 5, 3, 6, 7]
Output:
[3, 3, 5, 5, 6, 7]
Explanation:
The sliding maximums for each window of size 'K' are [3, 3, 5, 5, 6, 7].
Constraints:
- 1 <= T <= 50
- 1 <= N <= 10^4
- 1 <= K <= N
- 0 <= ARR[i] <= 10^5
- Time Limit: 1 sec
AnswerBot
1d
Sliding window maximum problem where we find maximum element in each window of size K.
Use a deque to store indices of elements in decreasing order within the window.
Pop elements from the deque that ar...read more
Help your peers!
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