-
Notifications
You must be signed in to change notification settings - Fork 16
/
queue3.c
48 lines (47 loc) · 963 Bytes
/
queue3.c
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
/*
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
A[]= {1,2,3,3,4,5,2,1,6}
k = 3
{3,3,4,5,5,5,6}*/
#include<stdio.h>
int queue[100];
int front = -1, rear = -1;
void insert (int size, int value) {
int item;
if(rear == size-1)
printf("Queue overflow");
else {
if(front == -1)
front = 0;
rear = rear + 1;
queue[rear] = value;
}
}
void display(int value) {
int index, jindex, count = 0,max = -1000;
if(front == -1)
printf("Queue empty");
else {
for(index = front; index <= rear- value + 1; index ++) {
count = 0;
max = -1000;
for(jindex = index; count < value; jindex ++) {
if(queue[jindex] > max)
max = queue[jindex];
count ++;
}
printf("%d ",max);
}
printf("\n");
}
}
int main() {
int size,index,k,element;
scanf("%d",&size);
for(index = 0; index < size; index ++) {
scanf("%d",&element);
insert(size,element);
}
scanf("%d",&k);
display(k);
}