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
4mo

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!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at American Express

Q. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
Q. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits