I am getting segmentation error.But I can not understand the reason for it.The code is given below:-1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n)
{
vector v(n);
stack s;
for(int i=0;i<n;i++)
{
scanf("%d",&v[i]);
}
int j=1;
for(int i=0;i<v.size();i++)
{
while(j<=n)
{
if (v[i] > j)
{
s.push(v[i]);
v.erase(v.begin()+i);
}
else if (v[i]==j)
{
j++;
break;
}
}
}
while(!s.empty())
{
v.push_back(s.top());
s.pop();
}
int flag=1;
for(int i=0;i<n;i++)
{
if (v[i] != i+1 )
{
flag=0;
break;
}
}
if (flag)
printf("yes\n");
else
printf("no\n");
scanf("%d",&n);
}
return 0;
}