I was getting WA in this problem so re-wrote a fresh code and got AC. But i am curious to figure out why was i getting WA on the following code. if someone can hint me.
#include<stdio.h>
int stack[2000];
int main(){
int i, n, ans, top, x, require;
while(scanf("%d", &n)){
if(!n) break;
ans=1;
top=-1;
require=1;
for (i=0; i<n; i++){
scanf("%d", &x);
if(x==require){
require++;
continue;
}
else if(top>-1 && stack[top]==require){
while (top>-1 && stack[top]==require)
top--;
stack[++top]=x;
require++;
continue;
}
else if(top>-1 && stack[top]<x){
while(++i<n){
scanf("%d", &x);
}
ans=0;
break;
}
else
stack[++top]=x;
}
if(ans) printf("yes\n");
else printf("no\n");
}
return 0;
}
Thanks