#include <stdio.h>
int main() {
int num, i, j, flag = 0;
long test=1;
int *arr, *rep;
while (scanf("%ld", &test), test != 0) {
arr = (int *)malloc(sizeof(int)*(test+1));
rep = (int *)malloc(sizeof(int)*(test+1));
for (j=1;j<=test;j++) {
scanf("%d",&arr[j]);
}
for (j=1;j<=test;j++) {
rep[arr[j]] = j;
}
for (j=1;j<=test;j++)
if (rep[j] == arr[j]) {
printf("ambiguous\n");
flag = 1;
break;
}
if (!flag)
printf("not ambiguous\n");
flag = 0;
}
return 0;
}
I create a new malloc mem allocation each test case, store the values in a array (arr) and apply inverse permutation and the answer is stored in another array (rep). I checked by printing out ‘rep’ array and all the answers for the given test cases are correct. But codechef says that they are wrong answers. Help.