You are not logged in. Please login at www.codechef.com to post your questions!

×

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;
}

asked 18 May '12, 22:50

dalphin's gravatar image

1★dalphin
1111
accept rate: 0%

edited 19 May '12, 04:37

cyberax's gravatar image

2★cyberax ♦
3.4k21955


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;
}
link

answered 21 May '12, 10:30

ritesh_gupta's gravatar image

5★ritesh_gupta ♦
3.7k42549
accept rate: 27%

edited 21 May '12, 10:36

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×587
×165

question asked: 18 May '12, 22:50

question was seen: 1,008 times

last updated: 13 Feb, 23:18