problem in Ambiguous problem

#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++) {
    for (j=1;j<=test;j++) {
        rep[arr[j]] = j;
    for (j=1;j<=test;j++)
        if (rep[j] == arr[j]) {
            flag = 1;
    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.

Your code would check for first element of both the arrays. If both are same, ambiguous would be printed. You need to make sure that till last element, they are same.

So, my advice would be to check if both current array elements are equal or not ?
if they are, move on to next element to check it is same.
if they are not, you need to break out of loop as this can’t be ambiguous any more.

1 Like