Working fine on my PC, but showing wrong output on CodeChef

I have written the following program for PERMUT2- ambiguous permutations
And I simply don’t understand why this code says wrong output on CodeChef, but runs fine on my PC…

#include
#include <malloc.h>
#include <stdio.h>

using namespace std;

int main() {
long int t, n;
int *permut, *inverse;
int ambFlag=1;

//cin>>t;

while (scanf("%d", &n) && n)	{
	//cin>>n;
	//if (n==0) break;
	
	permut=new int[n];
	inverse=new int[n];
	
	for (long int i=0; i<n; i++)	{
		cin>>permut[i];
		if (n==1) {
			ambFlag=1;
		}
		else {
			inverse[permut[i]-1]=i+1;
		}
	}
	
	if (n!=1)	{
		for (long int i=0; i<n; i++)	{
			if (permut[i]!=inverse[i]) {
				ambFlag=0; 
				break;
			}
		}
	}
		
	if (ambFlag==0) cout<<"not ambiguous"<<endl;
	else cout<<"ambiguous"<<endl;
	
	delete [] permut;
	delete [] inverse;
}
	
return 0;

}

#include
#include <malloc.h>
#include <stdio.h>
using namespace std;

int main() {
long int t, n;
int *permut, *inverse;
int ambFlag=1;

//cin>>t;

while (scanf("%d", &n) && n) {
//cin>>n;
//if (n==0) break;
ambFlag = 1;
permut=new int[n];
inverse=new int[n];

for (long int i=0; i<n; i++)    {
    cin>>permut[i];
    if (n==1) {
        ambFlag=1;
    }
    else {
        inverse[permut[i]-1]=i+1;
    }
}

if (n!=1)   {
    for (long int i=0; i<n; i++)    {
        if (permut[i]!=inverse[i]) {
            ambFlag=0; 
            break;
        }
    }
}

if (ambFlag==0) cout<<"not ambiguous"<<endl;
else cout<<"ambiguous"<<endl;

delete [] permut;
delete [] inverse;

}

return 0;
}

you are not resetting ambFlag=1;
every test case…
once it become zero it wont serve any purpose
so add ambFlag = 1 as the first statement in the loop

#include<stdio.h>
#include<stdlib.h>
short int dec[1000]={0};
int main()
{int n,j=0;
scanf("%d",&n);
while(1)
{if(n==0)break;
short int src=NULL;
src=(short int
)malloc(n*sizeof(short int));
int i;
for(i=0;i<n;i++)
{scanf("%hd",(src+i));
}
for(i=0;i<n;i++)
{
if(src[src[i]-1]!=i+1)
break;
}
if(i==n) dec[j++]=1;
else dec[j++]=2;
scanf("%d",&n);
free(src);
}
j=0;
while(dec[j]!=0)
{if(dec[j++]==1)
printf(“ambiguous\n”);else printf(“not ambiguous\n”);}

return 0;
}

I am getting SIGSEGV on this code.
Please help i am unable to figure it out.

Please help me too. The following code has been judged wrong although it outputs the exact inverse permutation. I posted a comment below that question and a user answered saying that the second half of my “decide” method is wrong but I couldn’t figure that out. Please help.

http://www.codechef.com/viewsolution/3914108

// factorial
#include

using namespace std;

int main()
{
long double t, n;
cin >> t;
for (int i=0; i<t; i++)
{
long int c=0;
cin >> n;
for (int j=1; int(n/(5^j))>0; j++)
c = c + int(n/(5^j));
cout << c << endl;
}
return 0;
}
for this code codechef shows wrong answer

include

using namespace std;
int main()
{
int t,n;
cin >> t;
while(t–)
{
cin >> n;
int count=0;
for(int j=5;(int)(n/j)>=1;j*=5)
{
count=count+(int)(n/j);
}
cout << count << “\n”;

}
return 0;
}

and for this it is accepted although both of them are giving same output.
Plz someone rectify the mistake in first code snippet

http://ww2.codechef.com/viewsolution/2146773

I am getting SIGSEGV on this code.
Please help.

I fixed several problems in your solution, and it’s ok now - CodeChef: Practical coding for everyone

Once observe this part :

for (i = 0; i < len; i++)

{

    if (newseq[i] != seq[i])

            return 1;

    else

            return 0;

}

every time you are checking only the first terms i.e you are returning 1
if (newseq[0] != seq[0])
and 0 for
(newseq[0] == seq[0]).
Here is the mistake. i.e you need to check all the terms and then return 0 if all terms are equal but not only the first term. If you have any doubt try this case:

6

1 3 5 6 2 4

correct answer is not ambiguous

but your answer is ambiguous

Oh I understood that now. I wanted it to output as soon as a discrepancy between the given permutation and the inverse one is found. So now I have used a counter j, but it is still judged as a wrong answer.

http://www.codechef.com/viewsolution/3929621

it should not be j==len-1
but j==len
i.e
if (j == len)
return 0;
or simply
return 0;

http://www.codechef.com/viewsolution/3929637

Thank you so much. This question had been bothering me for last 3 days.

You are welcome :slight_smile: