Question: would someone please explain this example and its answer. I understand if an array has {3,2,6} then its inverse would be {6,2,3}. Is this what being asked because the piece of code below doesn't seem to achieve this?

Suppose that the array int pi[128] contains a permutation of the numbers 0, 1, ...., 127.

What one or two lines of Java code would you use to efficiently produce the inverse of
pi in the array ip[128]?

**ANSWER:** 

```
 for (i=0; i<128; ++i) 
      ip[pi[i]] = i

```

Answer: 
Don't know where you saw that answer, because it is wrong.

To illustrate, let's use array of size 4.  

With `pi = {3,0,1,2}`, result is `ip = {1,2,3,0}`.  

Or `pi = {0,1,2,3}` becomes `ip = {0,1,2,3}`.  

Don't know in what definition of the word "inverse", those would be the right results.

Correct answer is:

```
for (int i = 0; i < 128; ++i)
    ip[i] = pi[127 - i];

```
The answer is wrong (and doesn't even compile).

The `ip` array will contain the *index* of each number, not the "inverse".

In the case where the permutation is ordered, the resultant array will be identical to the input:

```
int[] pi = {0, 1, 2, 3, 4};
int[] ip = new int[5];
for (int i = 0; i < 5; ++i)
    ip[pi[i]] = i;
System.out.println(Arrays.toString(ip));

```

Output:

```
[0, 1, 2, 3, 4]

```

Whatever "inverse" means, I wager it does't mean "the same". Your example strongly suggests it means "reverse", which the "answer" clearly does not produce.

---

Based on your question, IMHO your professor lacks knowledge, experience and is not the sharpest knife in the drawer. My advice is to find another teacher as quickly as you can.