5.10 Apply permutation - Couldn't understand the logic

#1

Hi all,

I couldn’t understand the logic mentioned in the book which solves in O(n) time and O(1) space complexity. Can anyone point me to some youtube link or some other resource where there are more examples explained? I really want to understand the logic behind this question.

Thanks in advance.

Edit : I now understood the basic idea behind this problem’s solution. Can anyone explain me the test-case result?

image

Shouldn’t it be what I got in the result? What I understand is, after applying permutation, A[0] should be what it was in A[7] before applying permutation. Am I right?

0 Likes

#2

Actually it is other way round, A[0] goes to index 7, A[1] goes to index 2, A[2] goes to index 11 etc. as mentioned in the book, new array B has the value as B[perm[i]] = A[i]

0 Likes

#3

Oh okay, I thought the reverse. I thought A[7] should be on A[0], A[2] should be on A[1] and so on. Thank you for clarifying.

0 Likes