Maximum of All Subarrays of Size K

Given an array of non-negative integers and an integer K representing the length of a subarray, your task is to determine the maximum elements for each subarray of size K.

Example:

Input:
T = 1
N = 6, K = 3
Array = [1, 3, 1, 2, 5, 3]
Output:
3 3 5 5
Explanation:

The subarrays are: [1, 3, 1], [3, 1, 2], [1, 2, 5], [2, 5, 3] - Maximums are 3, 3, 5, 5 respectively.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • 1 <= K <= N
  • 1 <= arr[i] <= 109
Note:
Just implement the function as required; no need to handle input or output.
AnswerBot
1mo

Find the maximum elements for each subarray of size K in a given array.

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

  • Pop elements from the deque t...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Engineer 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