Permute Elements of An Array
Example
Input: A = [a,b,c,d], P = [3,2,1,0]
Output: [d,c,b,a]Solution
def apply_permutation(A, P):
for i in range(len(A)):
next = i
while P[next] >= 0 :
A[next], A[P[next]] = A[P[next]], A[next]
next = P[next]
# make P[next] negative once it is used
P[next] -= len(A)
return ALast updated