forked from Vishal-Aggarwal0305/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxOfSubarrays.cpp
57 lines (44 loc) · 1.19 KB
/
maxOfSubarrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<int> ans = max_of_subarrays(arr, n, k);
for(auto x: ans) cout<<x<<" ";
cout<<endl;
}
return 0;
}
vector <int> max_of_subarrays(int *arr, int n, int k)
{
vector <int> ans;
list <int> l;
int i=0, j=0;
//If K > length of the array, return single element which is the max of the array.
// if(k>n) {
// ans.push_back(*max_element(arr.begin(),arr.end()));
// return ans;
// }
//sliding window.
while(j<n){
while(l.size() > 0 && l.back()<arr[j]){
l.pop_back();
}
l.push_back(arr[j]);
if(j-i+1 < k) j++;
else if(((j-i)+1) == k){
ans.push_back(l.front());
if(arr[i] == l.front()) l.pop_front();
i++;
j++;
}
}
return ans;
}