# Permute the elements of an array following given order

A permutation is a rearrangement of members of a sequence into a new sequence. For example, there are 24 permutations of [a, b, c, d]. Some of them are [b, a, d, c], [d, a, b, c] and [a, d, b, c].
A permutation can be specified by an array P[] where P[i] represents the location of the element at index i in the permutation.

For example, the array [3, 2, 1, 0] represents the permutation that maps the element at index 0 to index 3, the element at index 1 to index 2, the element at index 2 to index 1 and the element at index 3 to index 0.

Given the array arr[] of N elements and a permutation array P[], the task is to permute the given array arr[] based on the permutation array P[].

Examples:

Input: arr[] = {1, 2, 3, 4}, P[] = {3, 2, 1, 0}
Output: 4 3 2 1

Input: arr[] = {11, 32, 3, 42}, P[] = {2, 3, 0, 1}
Output: 3 42 11 32

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Every permutation can be represented by a collection of independent permutations, each of which is cyclic i.e. it moves all the elements by a fixed offset wrapping around. To find and apply the cycle that indicates entry i, just keep going forward (from i to P[i]) till we get back at i. After completing the current cycle, find another cycle that has not yet been applied. To check this, subtract n from P[i] after applying it. This means that if an entry in P[i] is negative, we have performed the corresponding move.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to permute the the given ` `// array based on the given conditions ` `int` `permute(``int` `A[], ``int` `P[], ``int` `n) ` `{ ` `    ``// For each element of P ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``int` `next = i; ` ` `  `        ``// Check if it is already ` `        ``// considered in cycle ` `        ``while` `(P[next] >= 0) { ` ` `  `            ``// Swap the current element according ` `            ``// to the permutation in P ` `            ``swap(A[i], A[P[next]]); ` `            ``int` `temp = P[next]; ` ` `  `            ``// Subtract n from an entry in P ` `            ``// to make it negative which indicates ` `            ``// the corresponding move ` `            ``// has been performed ` `            ``P[next] -= n; ` `            ``next = temp; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A[] = { 5, 6, 7, 8 }; ` `    ``int` `P[] = { 3, 2, 1, 0 }; ` `    ``int` `n = ``sizeof``(A) / ``sizeof``(``int``); ` ` `  `    ``permute(A, P, n); ` ` `  `    ``// Print the new array after ` `    ``// applying the permutation ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << A[i] << ``" "``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to permute the the given ` `// array based on the given conditions ` `static` `void` `permute(``int` `A[], ``int` `P[], ``int` `n) ` `{ ` `    ``// For each element of P ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``int` `next = i; ` ` `  `        ``// Check if it is already ` `        ``// considered in cycle ` `        ``while` `(P[next] >= ``0``) ` `        ``{ ` ` `  `            ``// Swap the current element according ` `            ``// to the permutation in P ` `            ``swap(A, i, P[next]); ` `            ``int` `temp = P[next]; ` ` `  `            ``// Subtract n from an entry in P ` `            ``// to make it negative which indicates ` `            ``// the corresponding move ` `            ``// has been performed ` `            ``P[next] -= n; ` `            ``next = temp; ` `        ``} ` `    ``} ` `} ` ` `  `static` `int``[] swap(``int` `[]arr, ``int` `i, ``int` `j) ` `{ ` `    ``int` `temp = arr[i]; ` `    ``arr[i] = arr[j]; ` `    ``arr[j] = temp; ` `    ``return` `arr; ` `}  ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `A[] = { ``5``, ``6``, ``7``, ``8` `}; ` `    ``int` `P[] = { ``3``, ``2``, ``1``, ``0` `}; ` `    ``int` `n = A.length; ` ` `  `    ``permute(A, P, n); ` ` `  `    ``// Print the new array after ` `    ``// applying the permutation ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``System.out.print(A[i]+ ``" "``); ` ` `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python 3

 `# Python 3 implementation of the approach ` ` `  `# Function to permute the the given ` `# array based on the given conditions ` `def` `permute(A, P, n): ` `     `  `    ``# For each element of P ` `    ``for` `i ``in` `range``(n): ` `        ``next` `=` `i ` ` `  `        ``# Check if it is already ` `        ``# considered in cycle ` `        ``while` `(P[``next``] >``=` `0``): ` `             `  `            ``# Swap the current element according ` `            ``# to the permutation in P ` `            ``t ``=` `A[i] ` `            ``A[i] ``=` `A[P[``next``]] ` `            ``A[P[``next``]] ``=` `t ` `             `  `            ``temp ``=` `P[``next``] ` ` `  `            ``# Subtract n from an entry in P ` `            ``# to make it negative which indicates ` `            ``# the corresponding move ` `            ``# has been performed ` `            ``P[``next``] ``-``=` `n ` `            ``next` `=` `temp ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``A ``=` `[``5``, ``6``, ``7``, ``8``] ` `    ``P ``=` `[``3``, ``2``, ``1``, ``0``] ` `    ``n ``=` `len``(A) ` ` `  `    ``permute(A, P, n) ` ` `  `    ``# Print the new array after ` `    ``# applying the permutation ` `    ``for` `i ``in` `range``(n): ` `        ``print``(A[i], end ``=` `" "``) ` `         `  `# This code is contributed by Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function to permute the the given  ` `    ``// array based on the given conditions  ` `    ``static` `void` `permute(``int` `[]A, ``int` `[]P, ``int` `n)  ` `    ``{  ` `        ``// For each element of P  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{  ` `            ``int` `next = i;  ` `     `  `            ``// Check if it is already  ` `            ``// considered in cycle  ` `            ``while` `(P[next] >= 0)  ` `            ``{  ` `     `  `                ``// Swap the current element according  ` `                ``// to the permutation in P  ` `                ``swap(A, i, P[next]);  ` `                ``int` `temp = P[next];  ` `     `  `                ``// Subtract n from an entry in P  ` `                ``// to make it negative which indicates  ` `                ``// the corresponding move  ` `                ``// has been performed  ` `                ``P[next] -= n;  ` `                ``next = temp;  ` `            ``}  ` `        ``}  ` `    ``}  ` `     `  `    ``static` `int``[] swap(``int` `[]arr, ``int` `i, ``int` `j)  ` `    ``{  ` `        ``int` `temp = arr[i];  ` `        ``arr[i] = arr[j];  ` `        ``arr[j] = temp;  ` `        ``return` `arr;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]A = { 5, 6, 7, 8 };  ` `        ``int` `[]P = { 3, 2, 1, 0 };  ` `        ``int` `n = A.Length;  ` `     `  `        ``permute(A, P, n);  ` `     `  `        ``// Print the new array after  ` `        ``// applying the permutation  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `            ``Console.Write(A[i]+ ``" "``);  ` `     `  `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```8 7 6 5
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.