-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubarrays (using deque).cpp
71 lines (42 loc) · 1.09 KB
/
Subarrays (using deque).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
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
int main()
{
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
ifstream cin("/home/aditya/Documents/ip");
#endif
int n,k;
cin>>n;
vector<int> v;
int val;
for(int i=0;i<n;i++)
{
cin>>val;
v.push_back(val);
}
cin>>k;
deque<int> dq;
///first store the useful element among first k elements and obtain the maximum of them
for(int i=0;i<k;i++)
{
while(!dq.empty() and v[i]>=v[dq.back()])
dq.pop_back();
dq.push_back(i);
}
///Now traverse from i=k;
for(int i=k;i<n;i++)
{
///first advance the left pointer ,to obtain the current window left pointer
cout<<v[dq.front()]<<" ";
while(!dq.empty() and dq.front()<=i-k)
dq.pop_front();
///so far we have adjusted the left
///Now add next element to obtain the current window
while(!dq.empty() and v[i]>= v[dq.back()])
dq.pop_back();
dq.push_back(i);///we have stored all the elements for the current window ,and we'll print the maximum of this window in the next iteration
}
///to print aswer for last window
cout<<v[dq.front()];
}