Skip to content

Commit 0ad806f

Browse files
Deque Problem
1 parent 25a6a83 commit 0ad806f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

HackerRank Practice/deque.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
3+
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
4+
#define f(i,a,b) for(auto i=a;i<b;i++)
5+
#define fi(i,a,b,x) for(auto i=a;i<b;i=i+x)
6+
#define fe(i,a,b) for(auto i=a;i<=b;i++)
7+
#define fr(i,a,b) for(auto i=a;i>=b;i--)
8+
#define loop(i, a) for(auto i=a.begin();i!=a.end();i++)
9+
#define endl '\n'
10+
#define sp '\t'
11+
#define ll long long int
12+
#define ff first
13+
#define ss second
14+
#define pb push_back
15+
#define mp make_pair
16+
#define all(x) x.begin(), x.end()
17+
#define clr(x) memset(x, 0, sizeof(x))
18+
#define sortall(x) sort(all(x))
19+
#define mod 1000000007
20+
#define PI 3.1415926535897932384626
21+
#define deb(x) cout<<#x<<"="<<x<<endl
22+
#define deb2(x, y) cout<<#x<<" = "<<x<<","<<#y<<" = "<<y<<endl
23+
24+
using namespace std;
25+
26+
template <typename T>
27+
void print(vector<T>& a, char sep)
28+
{
29+
for(auto i : a) { cout<<i<<sep; }
30+
}
31+
32+
ll mod_opr(ll num)
33+
{
34+
return (num + mod) % mod;
35+
}
36+
37+
template <typename T>
38+
bool compare(T x, T y)
39+
{
40+
return x > y ? true : false;
41+
}
42+
43+
void printKMax(int arr[], int n, int k){
44+
45+
deque<int> de(k);
46+
int i;
47+
f(i, 0, k){
48+
while(!de.empty() && arr[i] >= arr[de.back()])
49+
de.pop_back();
50+
de.push_back(i);
51+
}
52+
for(;i<n;i++){
53+
cout<<arr[de.front()]<<" ";
54+
while((!de.empty()) && (de.front() <= i - k))
55+
de.pop_front();
56+
while((!de.empty()) && (arr[i] >= arr[de.back()]))
57+
de.pop_back();
58+
de.push_back(i);
59+
}
60+
cout<<arr[de.front()]<<endl;
61+
}
62+
63+
int main()
64+
{
65+
66+
// ONLINE_JUDGE
67+
#ifndef rahulbordoloi
68+
freopen("input.txt", "r", stdin);
69+
// freopen("output.txt", "w", stdin);
70+
#endif
71+
72+
// Code
73+
fast;
74+
int tc;
75+
cin>>tc;
76+
while(tc--){
77+
int n, k, i;
78+
cin>>n>>k;
79+
int arr[n];
80+
f(i,0,n) { cin>>arr[i]; }
81+
printKMax(arr, n, k);
82+
}
83+
84+
// cerr<<"TIME : "<<clock()<<endl;
85+
return 0;
86+
87+
}

0 commit comments

Comments
 (0)