@leppy no i m checking first time stack empty or not ..and pushing..
plz have a look here is my code..
#include<stdio.h>
#include<string.h>
void push(int arr[],int *top,int a)
{
(*top)++;
arr[*top]=a;
}
void pop(int *top)
{
(*top)--;
}
int main()
{
int n,j,i,count=1,flag=1;
int arr[1050],top=-1,ar[1050];
scanf("%d",&n);
while(n!=0)
{
for(i=1;i<=n;i++)
{
scanf("%d",&ar[i]);
}
for(i=1;i<=n;i++)
{
if(ar[i]==count)
{
count++;
continue;
}
else
{
if(top==-1) //cheking stack not empty..
{
push(arr,&top,ar[i]);
continue;
}
if(arr[top]==count)
{
while(arr[top]==count)
{
pop(&top);
count++;
}
}
if(arr[top]<ar[i])
{
printf("NO\n");
flag=0;
break;
}
else if(arr[top]>ar[i])
{
push(arr,&top,ar[i]);
}
}
}
if(flag)
printf("YES\n");
top=-1;
count=1;
flag=1;
scanf("%d",&n);
}
return 0;
}
and do u think setting top=-1;
will work as a empty stack or we have to make a loop to pop element one by one..