Maximum Of All Subarrays Of Size k.
You are given an array consisting of N non-negative integers, and an integer K denoting the length of a subarray, your task is to determine the maximum elements for each subarray of size K.
Note:
A subarray is a contiguous subset of an array.
The array may contain duplicate elements.
The given array follows 0-based indexing.
It is guaranteed that there exists at least one subarray of size K.
Input Format:
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains two space-separated integers N and K, as described in the problem statement.
The second line of each test case contains N space-separated integers, representing the elements of the array.
Output Format:
For each test case print in a new line, X space-separated integers, where X is the number of possible subarrays of size K, and the ith integer denote the maximum element of the ith possible subarray of size K starting from the left.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^5
1 <= K <= N
1 <= arr[i] <= 10^9
Time Limit: 1sec
CodingNinjas
author
2y
Brute force approach
- The Idea behind this brute force approach is to consider each subarray of size K by fixing a point i in the array and then consider all the points after it till we have considered ...read more
CodingNinjas
author
2y
Set Based Approach
- In the previous approach for finding the maximum element in the subarray we were iterating in the subarray of length K one by one, but we can optimize this step simply by using some ...read more
CodingNinjas
author
2y
Deque Based Approach
- In this approach, we will be using deque as we can further optimize the solution by deque.
- The idea for this approach is that we will keep only those elements that are candidates fo...read more
Add answer anonymously...
Top Travclan Technology India Technical Trainee interview questions & answers
Popular interview questions of Technical Trainee
>
Travclan Technology India Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app