Sliding Maximum Problem Statement

Given an array of integers ARR of length 'N' and a positive integer 'K', find the maximum elements for each contiguous subarray of size K.

Example:

Input:
ARR = [3, 4, -1, 1, 5]
K = 3
Output:
[4, 4, 5]
Explanation:

The maximum elements of the subarrays are: [4, 4, 5]. For each subarray: first is [3, 4, -1] with maximum 4, second is [4, -1, 1] with maximum 4, and third is [-1, 1, 5] with maximum 5.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • -105 <= arr[i] <= 105
  • 1 <= K <= N

Input:

The first line of input contains a single integer 'T', representing the number of test cases. Each test case consists of two lines. The first line contains two space-separated integers: 'N' (array length) and 'K' (subarray size). The second line contains 'N' space-separated integers, representing the elements of the array.

Output:

For each test case, output 'X' space-separated integers, where 'X' is the number of subarrays of size 'K'. Each integer represents the maximum element in its respective subarray.
Note:
You do not need to print anything; just implement the relevant function.
AnswerBot
7d

Implement a function to find maximum elements for each contiguous subarray of size K in an array of integers.

  • Iterate through the array and maintain a deque to store the indices of elements in decreasi...read more

Help your peers!
Add answer anonymously...
American Express Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter