 # Getting wrong answer in the problem code:PERMUT2

i am getting wrong answer in the problem ambiguous permutations but i think my code is correct and even in codeblocks it is giving correct output.

below is the link to code:

https://www.codechef.com/viewsolution/35872121

n can be as big as 10^5. You cannot store such a big length number in `int.` You can maybe use a string and just append the number as a character.

But an easier was to do this would be:

``````	int n;
cin >> n;
int A[n], B[n];
for(int i = 0; i < n; i++) {
cin >> A[i];
A[i]--;
B[A[i]] = i;
}
bool ok = 0;
for(int i = 0; i < n; i++) {
if(A[i] != B[i])
ok = 1;
}
cout << (ok ? "not ambigous\n" : "ambigous\n");
``````

You code will only work for n < 10, after which there will be overflows. You cannot represent 11 digit numbers (or larger) in datatype `int`.

3 Likes

but if i am not assigning the array size first and then getting it assigned after cin>>t, thenalso it is showing wrong answer

below is the updated code

``````#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int t,orig=0,raise;

while(1){
orig=0;

cin>>t;
int a[t];
if(t==0) break;
raise=t;
for(int i=0;i<t;i++)
{ //input
cin>>a[i];
orig+=a[i]*pow(10,--raise);
}

int total=0,power=t;

for(int j=1;j<=t;j++){ //reverse number storage loop

int count=0;

for(int i=0;i<t;i++){ //checking of the place loop

count++;
if(j==a[i])
{
break;
}
}

total=total+count*pow(10,--power);
//storing reverse value
}

if(total==orig) {cout<<"ambiguous"<<endl;}
else{cout<<"not ambiguous"<<endl;}
}
return 0;
}``````

I am taking about your `orig`. It has n digits. And an `int` can only have 9 digits. So when n > 10, you have overflow in `orig`.

Add this optimization flag to detects overflows.

3 Likes

ohh , i got it , now i undertood why my code getting wrong answer.
thank you sooo much for the help.

1 Like