-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraysub.cpp
43 lines (41 loc) · 990 Bytes
/
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
#include<cstdio>
int main() {
int n;
scanf("%d", &n);
int *array = new int[n];
for(int i=0;i<n;++i) {
scanf("%d", array + i);
}
int k;
scanf("%d", &k);
int curMax = array[0];
int maxIndex = 0;
for(int i=1;i<k - 1;++i) {
if(array[i] > curMax) {
curMax = array[i];
maxIndex = i;
}
}
for(int i=k-1;i<n;++i) {
int val = array[i];
// check if previous max is in range
int lIndex = i - (k - 1);
if(maxIndex >= lIndex) {
if(val > curMax) {
curMax = val;
maxIndex = i;
}
} else {
curMax = array[lIndex];
maxIndex = lIndex;
for(int j=lIndex + 1;j<=i;++j) {
if(array[j] > curMax) {
curMax = array[j];
maxIndex = j;
}
}
}
printf("%d ", curMax);
}
printf("\n");
}