Given a permutation of numbers from 1 to N, We can represent it in two ways.

For example, let us consider a permutation from 1 to 5

P = [2 4 1 5 3]

This representation directly indicates the position of each number. i.e 2 is first, 4 is second and so on.

Alternatively, this can also be represented in inverse permutation form.

A = [3 1 5 2 4]

Assuming 1-based index, each number at the

There are some cases when the actual permutation and the inverse permutation are same. We call it as an

The solution is simple. We have to create an inverse permutation in another array, and check if it is same as the original permutation. Calculating the inverse permutation is discussed in my previous post.

Following is the C++ code. It runs in O(n) time and O(n) space.

For example, let us consider a permutation from 1 to 5

P = [2 4 1 5 3]

This representation directly indicates the position of each number. i.e 2 is first, 4 is second and so on.

Alternatively, this can also be represented in inverse permutation form.

A = [3 1 5 2 4]

Assuming 1-based index, each number at the

*index 'i'*indicates that the*number 'i'*is positioned at A[i] in the actual permutation. So this means that 1 appears at index 3, 2 appears at index 1, and so on.There are some cases when the actual permutation and the inverse permutation are same. We call it as an

*ambiguous permutation*. Now the problem is how do we check if the give permutation is an ambiguous permutation?The solution is simple. We have to create an inverse permutation in another array, and check if it is same as the original permutation. Calculating the inverse permutation is discussed in my previous post.

Following is the C++ code. It runs in O(n) time and O(n) space.