Inverse Permutation

Given an array of size n of integers in range from 1 to n, we need to find the inverse permutation of that array.

An inverse permutation is a permutation which you will get by inserting position of an element at the position specified by the element value in the array. For better understanding, consider the following example:
Suppose we found element 4 at position 3 in an array, then in reverse permutation, we insert 3 (position of element 4 in the array) in position 4 (element value).
Basically, An inverse permutation is a permutation in which each number and the number of the place which it occupies is exchanged.

The array should contain element from 1 to array_size.

Example 1 :

Input  = {1, 4, 3, 2}
Output = {1, 4, 3, 2}

In this, For element 1 we insert position of 1 from arr1 i.e 1 at position 1 in arr2. For element 4 in arr1, we insert 2 from arr1 at position 4 in arr2. Similarly, for element 2 in arr1, we insert position of 2 i.e 4 in arr2.

Example 2 :
Input  = {2, 3, 4, 5, 1}
Output = {5, 1, 2, 3, 4}

In this example, for element 2 we insert position of 2 from arr1 in arr2 at position 2. similarly, we find the inverse permutation of other elements.

Consider an array arr having elements 1 to n.

Method 1 :
In this method, we take element one by one and check elements in increasing order and print the position of the element where we find that element.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Naive CPP Program to find inverse permutation.
#include <bits/stdc++.h>
using namespace std;
  
// C++ function to find inverse permutations
void inversePermutation(int arr[], int size) {
  
  // Loop to select Elements one by one
  for (int i = 0; i < size; i++) {
  
    // Loop to print position of element
    // where we find an element
    for (int j = 0; j < size; j++) {
  
      // checking the element in increasing order
      if (arr[j] == i + 1) {
  
        // print position of element where
        // element is in inverse permutation
        cout << j + 1 << " ";
        break;
      }
    }
  }
}
  
// Driver program to test above function
int main() {
  int arr[] = {2, 3, 4, 5, 1};
  int size = sizeof(arr) / sizeof(arr[0]);
  inversePermutation(arr, size);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Naive java Program to find inverse permutation.
import java.io.*;
  
class GFG {
  
    // java function to find inverse permutations
    static void inversePermutation(int arr[], int size)
    {
        int i ,j;
        // Loop to select Elements one by one
        for ( i = 0; i < size; i++) 
        {
          
            // Loop to print position of element
            // where we find an element
            for ( j = 0; j < size; j++)
            {
          
                // checking the element in 
                // increasing order
                if (arr[j] == i + 1
                {
                    // print position of element
                    // where element is in inverse
                    // permutation
                    System.out.print( j + 1 + " ");
                    break;
                }
            }
        }
    }
      
    // Driver program to test above function
      
      
    public static void main (String[] args)
    {
        int arr[] = {2, 3, 4, 5, 1};
        int size = arr.length;
        inversePermutation(arr, size);
          
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Naive Python3 Program to
# find inverse permutation.
  
# Function to find inverse permutations
def inversePermutation(arr, size): 
  
    # Loop to select Elements one by one
    for i in range(0, size): 
  
        # Loop to print position of element
        # where we find an element
        for j in range(0, size): 
  
        # checking the element in increasing order
            if (arr[j] == i + 1): 
  
                # print position of element where
                # element is in inverse permutation
                print(j + 1, end = " "
                break
  
# Driver Code
arr = [2, 3, 4, 5, 1
size = len(arr)
  
inversePermutation(arr, size) 
  
#This code is cotributed by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Naive C# Program to find inverse permutation.
using System;
  
class GFG {
  
    // java function to find inverse permutations
    static void inversePermutation(int []arr, int size)
    {
        int i ,j;
        // Loop to select Elements one by one
        for ( i = 0; i < size; i++) 
        {
          
            // Loop to print position of element
            // where we find an element
            for ( j = 0; j < size; j++)
            {
          
                // checking the element in 
                // increasing order
                if (arr[j] == i + 1) 
                {
                    // print position of element
                    // where element is in inverse
                    // permutation
                    Console.Write( j + 1 + " ");
                    break;
                }
            }
        }
    }
      
    // Driver program to test above function
      
      
    public static void Main ()
    {
        int []arr = {2, 3, 4, 5, 1};
        int size = arr.Length;
        inversePermutation(arr, size);
          
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Naive PHP Program to 
// find inverse permutation.
  
// Function to find 
// inverse permutations
function inversePermutation($arr, $size
{
for ( $i = 0; $i < $size; $i++) 
{
  
    // Loop to print position of element
    // where we find an element
    for ($j = 0; $j < $size; $j++)
    {
  
    // checking the element 
    // in increasing order
    if ($arr[$j] == $i + 1) 
    {
  
        // print position of element 
        // where element is in 
        // inverse permutation
        echo $j + 1 , " ";
        break;
    }
    }
}
}
  
// Driver Code
$arr = array(2, 3, 4, 5, 1);
$size = sizeof($arr);
inversePermutation($arr, $size);
  
// This code is contributed by aj_36
?>

chevron_right



Output :

 5 1 2 3 4

Method 2 :
The idea is to to use another array to store index and element mappings

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient CPP Program to find inverse permutation.
#include <bits/stdc++.h>
using namespace std;
  
// C++ function to find inverse permutations
void inversePermutation(int arr[], int size) {
  
  // to store element to index mappings
  int arr2[size];
  
  // Inserting position at their
  // respective element in second array
  for (int i = 0; i < size; i++) 
    arr2[arr[i] - 1] = i + 1; 
  
  for (int i = 0; i < size; i++) 
    cout << arr2[i] << " ";  
}
  
// Driver program to test above function
int main() {
  int arr[] = {2, 3, 4, 5, 1};
  int size = sizeof(arr) / sizeof(arr[0]);
  inversePermutation(arr, size);
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java Program to find
// inverse permutation.
import java.io.*;
  
class GFG {
      
// function to find inverse permutations
static void inversePermutation(int arr[], int size) {
  
    // to store element to index mappings
    int arr2[] = new int[size];
  
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
  
    for (int i = 0; i < size; i++)
    System.out.print(arr2[i] + " ");
}
  
// Driver program to test above function
public static void main(String args[]) {
    int arr[] = {2, 3, 4, 5, 1};
    int size = arr.length;
    inversePermutation(arr, size);
}
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Efficient Python 3 Program to find
# inverse permutation.
  
# function to find inverse permutations
def inversePermutation(arr, size) :
  
    # To store element to index mappings
    arr2 = [0] *(size)
      
    # Inserting position at their
    # respective element in second array
    for i in range(0, size) :
        arr2[arr[i] - 1] = i + 1
      
    for i in range(0, size) :
        print( arr2[i], end = " "
      
# Driver program
arr = [2, 3, 4, 5, 1]
size = len(arr)
  
inversePermutation(arr, size)
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient C# Program to find
// inverse permutation.
using System;
  
class GFG {
      
// function to find inverse permutations
static void inversePermutation(int []arr, int size) {
  
    // to store element to index mappings
    int []arr2 = new int[size];
  
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
  
    for (int i = 0; i < size; i++)
    Console.Write(arr2[i] + " ");
}
  
// Driver program to test above function
public static void Main() {
    int []arr = {2, 3, 4, 5, 1};
    int size = arr.Length;
    inversePermutation(arr, size);
}
}
  
// This code is contributed by vt_m.

chevron_right


Output : 5 1 2 3 4


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.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


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