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
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 <bits/stdc++.h>using namespace std;// Function to permute the the given// array based on the given conditionsint 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 codeint 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 approachclass GFG{// Function to permute the the given// array based on the given conditionsstatic 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 codepublic 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 conditionsdef 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 codeif __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 approachusing 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 |
Javascript
<script>// JavaScript implementation of the approach// Function to permute the the given// array based on the given conditionsfunction permute(A, P, n) { // For each element of P for (let i = 0; i < n; i++) { let next = i; // Check if it is already // considered in cycle while (P[next] >= 0) { // Swap the current element according // to the permutation in P let x = A[i]; A[i] = A[P[next]]; A[P[next]] = x; let 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 codelet A = [5, 6, 7, 8];let P = [3, 2, 1, 0];let n = A.length;permute(A, P, n);// Prlet the new array after// applying the permutationfor (let i = 0; i < n; i++) document.write(A[i] + " "); </script> |
8 7 6 5
Time Complexity: O(n)
Space Complexity : O(1)



