#include <stdio.h>
#define max 1000

int q[max];
int top=-1;
int bot=-1;

int empty(void){
    if(top==bot) return 1;
    else return 0;
}
void push(int value){
    bot++;
    q[bot]=value;
}
int pop(){
    top++;
    return q[top];
}
int peek()
{
    return q[top+1];
}
int main() {
    int a;
    scanf("%d",&a);
    int x[1000];
    for(int i=0;i<a;i++)
    {
        scanf("%d",&x[i]);
    }
    int k=1,m=1,p=0;
    while(k<a)
    {
        if(x[p]==m && p<a)
        {
            k++,m++;
            p++;
            continue;
        }
        else if(peek()==m && empty()==0) 
        {
            k++,m++;
            pop();
            continue;
        }
        else if(empty()==1)
        {
            push(x[p]);
            p++;
            continue;
        }
        else if(peek()<x[p])
        {
            push(x[p]);
            p++;
            continue;
        }
        else
        {
            printf("NO");
            break;
        }
    }
    if(k==a)
    {
        printf("YES");
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: