-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAYSUB.cpp
77 lines (58 loc) · 1.71 KB
/
ARRAYSUB.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
66
67
68
69
70
71
72
73
74
75
76
77
//DATE: 16/06/2016
//Author: Ramjeet Saran
//http://www.spoj.com/problems/ARRAYSUB/
// ---- PROBLEM STATEMENT ---- //
/*
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Input
the number n denoting number of elements in the array then after a new line we have the numbers of the array
and then k in a new line
n<10^6
k<10^5
1 <= k <=n
and each element of the array is between 0 and 10^6
Output
print the output array
*/
//---- SOLUTION ----- //
/* USING DEQUEU O(n) method
We create a Dequeue, Qi of capacity k, that stores only useful elements of current window of k elements.
An element is useful if it is in current window and is greater than all other elements on
left side of it in current window. We process all array elements one by one and maintain Qi
to contain useful elements of current window and these useful elements are maintained in sorted order.
The element at front of the Qi is the largest and element at rear of Qi is the smallest of current window.
*/
# include <bits/stdc++.h>
# define gc getchar_unlocked
# define pc putchar_unlocked
# define li long int
# define lli long long int
using namespace std;
int main() {
li N, K;
cin>>N;
li arr[N];
for(li i = 0; i < N; i++) {
cin>>arr[i];
}
cin>>K;
deque<li> _dq(K);
li i = 0;
for(; i < K; i++) {
while(!(_dq.empty()) && arr[i] >= arr[_dq.back()]) {
_dq.pop_back();
}
_dq.push_back(i);
}
for(; i < N; i++) {
cout<<arr[_dq.front()]<<" ";
while(!(_dq.empty()) && _dq.front() <= i - K) {
_dq.pop_front();
}
while(!(_dq.empty()) && arr[i] >= arr[_dq.back()]) {
_dq.pop_back();
}
_dq.push_back(i);
}
cout<<arr[_dq.front()];
}