Inverse Permutation Problem

I am getting Wrong Answer prompt for the following code:

#include<stdio.h>
 
int main()
{

int t,n,i,flag;

while(1)
{
	flag=1;
	scanf("%d",&n);
	if(n==0)
	break;
	if(n>0 && n<=100000)
	{
		int arr[n];
		for(i=0;i<n;i++)
		{
			scanf("%d",&arr[i]);
		}
		for(i=0;i<n;i++)
		{
			if(arr[arr[i]-1]!=i+1)
				{	
					flag=0;
					break;
				}
			
		}
		if(flag==1)
		printf("ambiguous\n");
		else
		printf("non ambiguous\n");
		
	}
}
return 0;

}

and if possible Code optimization suggestions would be appreciated.

Took me time to find your problem in problemlist. It’s called “Ambiguous Permutations”.

They ask you to output “not ambiguous” instead of “non ambiguous”.

And you don’t have to check (n > 0 && n <= 100000), because it’s guaranteed that it’s always true.

2 Likes

Thanks man!