What's wrong here?(Ambiguous Permutation Problem)

This is the problem I am attempting.(Ambiguous Permutation in easy section) I believe my code is ok . I know that I can make another array and compare but isn’t that long? I’m new here, what am I missing? Thanks.

#include<iostream>

using namespace std;

int main()
{
    
    int n,flag;
    cin>>n;
    int a[100000];
    while(n!=0)
    {
           for(int y=0;y<n;y++)
           scanf("%d",&a[y]);
           flag=1;
           for(int i =0;i<n&&flag;i++)
           {
                int p = a[i];
                if((i+1)!= a[p-1])
                 flag--;
                }
                if(flag==1)
                cout<<"ambiguous";
                else if(flag==0)
                cout<<"not ambiguous";
                cin>>n;
                }     
                
        return 0;        
}

this is your corrected code…

#include<iostream>

using namespace std;

int main()
{

    int n,flag;
    cin>>n;
    int a[100000];
    while(n!=0)
    {
           for(int y=0;y<n;y++)
           scanf("%d",&a[y]);
           flag=1;
           for(int i =0;i<n&&flag;i++)
           {
                int p = a[i];
                if((i+1)!= a[p-1])
                 flag--;
                }
                if(flag==1)
                cout<<"ambiguous\n";
                else if(flag==0)
                cout<<"not ambiguous\n";
                cin>>n;
                }

        return 0;        
}

you had to print a new line after every ans…!!!

//