-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_window.c
More file actions
67 lines (57 loc) · 1.65 KB
/
max_window.c
File metadata and controls
67 lines (57 loc) · 1.65 KB
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
// this program stores the max of all subarray of length k (window sliding them , so n-k+1 in number) in O(n)
// #include <bits/stdc++.h>
// using namespace std;
#include <stdio.h>
void enque(int queue[], int *place, int i)
{
queue[*place] = i;
*place += 1;
}
int deque(int queue[] , int* place)
{
*place = *place -1;
return queue[*place];
}
int main()
{
int n;
scanf("%d",&n);
int Array[n];
for (int i = 0; i < n; i++)
scanf("%d",&Array[i]);
int k; // window size
// cin >> k;
scanf("%d",&k);
int answer[n - k + 1];
int queue[n]; // will store the k elements in it // storing all element in queue
int place = 0; // tells where we have to store
int index =0; // tells where we have to store in answer array
int start =0; // will be used when we want to know where max element of window is stored
//pushing first k element in the queue
enque(queue , &place , 0);
for (int i = 1; i < k; i++) // bottom will remain zero
{
while(place != 0 && Array[i] > Array[queue[place-1]])
{
deque(queue , &place);
}
enque(queue, &place, i);
}
answer[index++] = Array[queue[start]];
// now inserting element one by one and finding the answer of each window
for(int i=k;i<n;i++)
{
while(place != 0 && place > start && Array[i] > Array[queue[place-1]])
{
deque(queue , &place);
}
enque(queue, &place, i);
while(queue[start] < i-k+1)
{
start++;
}
answer[index++] = Array[queue[start]];
}
for(int i=0;i<n-k+1;i++)
printf("%d ", answer[i]);
}