whats wrong with this code for permutation problem?

#include<stdio.h>
#include<stdlib.h>

int main(){
int n, i,m=0;

scanf("%d",&n);
while(n!=0){
int a[n],b[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);

        for(i=0;i<n;i++)
        b[a[i]-1]=i+1;

        for(i=0;i<n;i++){
        if(a[i]==b[i])
        {m++;
         if(m==n){
        printf("ambiguous");
        m=0;};
        }
        else{
                printf("not ambiguous");
                break;
                m=0;
                };

                };

scanf("%d",&n);
};
return 0;
}

There are few errors in your code::

(1)The output that you are printing doesn’t matches the output format as mentioned in problem statement.
You are not printing a newline after “ambiguous” or “not ambiguous” {printf(“ambiguous”)};THE NEW LINE IS MISSING.This is the most common mistake committed by the beginners.
Please note that the code chef judge compile your code and generate the output in a plain text file and matches your output file with the expected (their official) output file,thus even an xtra white-space can results in wrong answer.So always make sure,your code generates the output precisely in the same format as mentioned in problem statement.

(2).There is a small mistake in this section of code

      {
       m++;
		  if(m==n)
		  {
		   printf("ambiguous");
		   m=0;
		  };
       }
            else{
            printf("not ambiguous");
            break;
            m=0;
            };
           };

You are not breaking printing ambiguous.So it will print multiple times and ofcourse the missing of newline character as i mentioned is blunder here.
I have corrected your code and it will get accepted now.This is the correct code::

 #include<stdio.h>
 #include<stdlib.h>

 int main()
 {
   int n, i,m=0,flag;

   scanf("%d",&n);
   while(n!=0)
   {
		   int a[n],b[n];
		   for(i=0;i<n;i++)
		   scanf("%d",&a[i]);

		   for(i=0;i<n;i++)
		   b[a[i]-1]=i+1;

		   flag=0;
		   for(i=0;i<n && flag==0;i++)
		   {
			if(a[i]!=b[i])
		    { 
			  flag=1;
			}
		   }
    
			if(flag!=1)
			printf("ambiguous\n");
			else
			printf("not ambiguous\n");
           

          scanf("%d",&n);
    }
     return 0;
}
3 Likes