include

int main()
{
int n;
scanf("%d",&n);
int i;

int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);

int k;
scanf("%d",&k);

int front,rear=0,count;
int s=0,top=0;
for(front=0;front<k;front++)
   {
    if(a[rear]<=a[front])
    {rear=front;
    top=0;
    s=0;}
    else if(a[front]>=s)
        {
            s=a[front];
            top=front-rear;
        }}

printf("%d ",a[rear]);

    count=front-rear-1;


    for(i=1;i<=n-k;i++)
    {   front=i+k-1;
        count++;
        if(a[rear]<a[front])
        {
            rear=front;
            count=0;
            s=0;
            top=0;
        }

        else if(a[front]>=s)
        {
            s=a[front];
            top=front-rear;
        }
        if(count==k)
        {
            rear=rear+top;
            count=front-rear;
            top=0;
            s=0;
        }

        printf("%d ",a[rear]);
    }
    printf("\n");

return 0;

}

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 13 5d

Want to read more? Browse other topics in C and C++ or view latest topics.