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

Find the maximum elements for each subarray of size 'K' in a given array of non-negative integers.

  • Iterate through the array and maintain a deque to store the indices of elements in decreasing order.

  • Po...read more

Help your peers!
Select
Add answer anonymously...

Travclan Technology India Technical Trainee interview questions & answers

A Technical Trainee was asked Q. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Technical Trainee was asked Q. Convert Min Heap to Max Heap Problem Statement Given an array representation of ...read more
A Technical Trainee was asked Q. Minimum Cost to Destination You are given an NxM matrix consisting of '0's and '...read more

Popular interview questions of Technical Trainee

A Technical Trainee was asked Q1. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Technical Trainee was asked Q2. Convert Min Heap to Max Heap Problem Statement Given an array representation of ...read more
A Technical Trainee was asked Q3. Minimum Cost to Destination You are given an NxM matrix consisting of '0's and '...read more
Travclan Technology India Technical Trainee 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