Maximum of All Subarrays of Size k

Given an array of 'N' non-negative integers and an integer 'K', your task is to find the maximum elements for each subarray of size 'K'.

Input:

The first line contains an integer 'T', the number of test cases.
The first line of each test case contains two integers 'N' and 'K'.
The second line of each test case contains 'N' integers representing the array.

Output:

For each test case, print a line with 'X' integers, where 'X' is the number of subarrays of size 'K'. Each integer denotes the maximum element of the respective subarray.

Example:

Input:
2
5 3
1 3 2 4 5
6 4
8 5 10 7 9 4
Output:
3 4 5
10 10 10

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ K ≤ N
  • 1 ≤ arr[i] ≤ 109
  • Time Limit: 1 sec

Note:

A subarray is a contiguous subset of an array and the array may contain duplicate elements. The array follows 0-based indexing and it’s guaranteed that at least one subarray of size 'K' exists.

AnswerBot
1y

The task is to find the maximum elements for each subarray of size K in a given array.

  • Iterate through the array and maintain a deque of indices of the maximum elements in the current window of size K....read more

Help your peers!
Select
Add answer anonymously...

Decimal Point Analytics Software Developer Intern interview questions & answers

A Software Developer Intern was asked 8mo agoQ. Given an array of integers, find the first repeating element in the array.
A Software Developer Intern was asked 8mo agoQ. Define the four pillars of OOPS.
A Software Developer Intern was asked Q. Rahul and Minimum Subarray Challenge Rahul, who is passionate about programming,...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 8mo agoQ1. Given an array of integers, find the first repeating element in the array.
A Software Developer Intern was asked 8mo agoQ2. Define the four pillars of OOPS.
A Software Developer Intern was asked Q3. Rahul and Minimum Subarray Challenge Rahul, who is passionate about programming,...read more
Decimal Point Analytics 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