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!
Add answer anonymously...
Decimal Point Analytics 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