Need to explain what is Ambiguous Permutations and why the solution works . c++

Please, explain what is Ambiguous Permutations.  why does the solution works ?  I mean I know how operator ^ works, I just do not understand how mathematically define ambiguous/ non ambiguous permutation.

May there is some youtube video.

//// Ambiguous Permutations
//All submissions for this problem are available.
//Some programming contest problems are really tricky : not only do they require a different output format from what you might have expected, but also the sample output does not show the difference.For an example, let us look at permutations.
//A permutation of the integers 1 to n is an ordering of these integers.So the natural way to represent a permutation is to list the integers in this order.With n = 5, a permutation might look like 2, 3, 4, 5, 1.
//However, there is another possibility of representing a permutation : You create a list of numbers where the i - th number is the position of the integer i in the permutation.Let us call this second possibility an inverse permutation.The inverse permutation for the sequence above is 5, 1, 2, 3, 4.
//An ambiguous permutation is a permutation which cannot be distinguished from its inverse permutation.The permutation 1, 4, 3, 2 for example is ambiguous, because its inverse permutation is the same.To get rid of such annoying sample test cases, you have to write a program which detects if a given permutation is ambiguous or not.
//Input Specification
//The input contains several test cases.
//The first line of each test case contains an integer n(1 ≤ n ≤ 100000).Then a permutation of the integers 1 to n follows in the next line.There is exactly one space character between consecutive integers.You can assume that every integer between 1 and n appears exactly once in the permutation.
//The last test case is followed by a zero.
//Output Specification
//For each test case output whether the permutation is ambiguous or not.Adhere to the format shown in the sample output.
//Sample Input
//1 4 3 2
//2 3 4 5 1
//Sample Output
//not ambiguous

#include <iostream>
using namespace std; 
int main()
	int n, i, j, k;
	long long int s;

	cin >> n;
	while (n)
		s = 0;
		for (j = 1;j <= n;j++)
			cin >> k;
			if (k != j)
				s ^= (k*j);
		(s == 0) ? printf("ambiguous\n") : printf("not ambiguous\n");

		cin >> n;
	return 0;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Here is the explanation of ambiguous permutation
Sample : 1 6 3 4 2 5
This sample is some permutation of 6 numbers(from 1 to 6).
Now, for the inverse you start looking from 1 to 6 the numbers in the example, I mean:
The number 1 in wich position is located at 1 6 3 4 2 5? It is located first. So, the first number in the inverse is 1.
Inverse so far: 1XXXXX
The number 2 in wich position is located at 1 6 3 4 2 5? It is located 5th. So, the second number in the inverse is 5.
Inverse so far:15XXXX
The number 6 in wich position is located at 1 6 3 4 2 5? It is located 2nd. So, the 6th position in the inverse is 2.
Inverse so far:153462
This means this permutation is not ambiguous. Because 1 6 3 4 2 5 !=1 5 3 4 6 2.
Now, with 1 4 3 2(from 1 to 4)
In wich position is located 1 at 1 4 3 2. It's first, so the first number in the inverse is 1
In wich position is located 2 at 1 4 3 2. It's 4th, so the second number in inverse is 4
In wich position is located 3 at 1 4 3 2. It's 3rd, so the third numberse in the inverse is 3
In wich position is located 4 at 1 4 3 2. It's 2nd, so the fourth number in the inverse is 2
1 4 3 2 = 1 4 3 2, so this is an ambiguos permutation.
Notice that you need start looking from 1 until n.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
Basically inverse of a permutation is the position of each number in ascending order. (if permutation is of numbers 1 to 5, then it's inverse is position of each number starting from 1 till 5)
And if inverse matches the input permutation then it is an ambiguous permutation.
Karrtik IyerSoftware ArchitectCommented:
Sunny, any response? Do you still need any help in understanding what is ambiguous permutation?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.