Inverse Permutation
Last Updated :
02 Mar, 2023
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void inversePermutation( int arr[], int size) {
for ( int i = 0; i < size; i++) {
for ( int j = 0; j < size; j++) {
if (arr[j] == i + 1) {
cout << j + 1 << " " ;
break ;
}
}
}
}
int main() {
int arr[] = {2, 3, 4, 5, 1};
int size = sizeof (arr) / sizeof (arr[0]);
inversePermutation(arr, size);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void inversePermutation( int arr[], int size)
{
int i ,j;
for ( i = 0 ; i < size; i++)
{
for ( j = 0 ; j < size; j++)
{
if (arr[j] == i + 1 )
{
System.out.print( j + 1 + " " );
break ;
}
}
}
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 4 , 5 , 1 };
int size = arr.length;
inversePermutation(arr, size);
}
}
|
Python3
def inversePermutation(arr, size):
for i in range ( 0 , size):
for j in range ( 0 , size):
if (arr[j] = = i + 1 ):
print (j + 1 , end = " " )
break
arr = [ 2 , 3 , 4 , 5 , 1 ]
size = len (arr)
inversePermutation(arr, size)
|
C#
using System;
class GFG {
static void inversePermutation( int []arr, int size)
{
int i ,j;
for ( i = 0; i < size; i++)
{
for ( j = 0; j < size; j++)
{
if (arr[j] == i + 1)
{
Console.Write( j + 1 + " " );
break ;
}
}
}
}
public static void Main ()
{
int []arr = {2, 3, 4, 5, 1};
int size = arr.Length;
inversePermutation(arr, size);
}
}
|
PHP
<?php
function inversePermutation( $arr , $size )
{
for ( $i = 0; $i < $size ; $i ++)
{
for ( $j = 0; $j < $size ; $j ++)
{
if ( $arr [ $j ] == $i + 1)
{
echo $j + 1 , " " ;
break ;
}
}
}
}
$arr = array (2, 3, 4, 5, 1);
$size = sizeof( $arr );
inversePermutation( $arr , $size );
?>
|
Javascript
<script>
function inversePermutation(arr, size)
{
let i ,j;
for ( i = 0; i < size; i++)
{
for ( j = 0; j < size; j++)
{
if (arr[j] == i + 1)
{
document.write( j + 1 + " " );
break ;
}
}
}
}
let arr = [2, 3, 4, 5, 1];
let size = arr.length;
inversePermutation(arr, size);
</script>
|
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Method 2: The idea is to use another array to store index and element mappings
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void inversePermutation( int arr[], int size) {
int arr2[size];
for ( int i = 0; i < size; i++)
arr2[arr[i] - 1] = i + 1;
for ( int i = 0; i < size; i++)
cout << arr2[i] << " " ;
}
int main() {
int arr[] = {2, 3, 4, 5, 1};
int size = sizeof (arr) / sizeof (arr[0]);
inversePermutation(arr, size);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void inversePermutation( int arr[], int size) {
int arr2[] = new int [size];
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] + " " );
}
public static void main(String args[]) {
int arr[] = { 2 , 3 , 4 , 5 , 1 };
int size = arr.length;
inversePermutation(arr, size);
}
}
|
Python3
def inversePermutation(arr, size) :
arr2 = [ 0 ] * (size)
for i in range ( 0 , size) :
arr2[arr[i] - 1 ] = i + 1
for i in range ( 0 , size) :
print ( arr2[i], end = " " )
arr = [ 2 , 3 , 4 , 5 , 1 ]
size = len (arr)
inversePermutation(arr, size)
|
C#
using System;
class GFG {
static void inversePermutation( int []arr, int size) {
int []arr2 = new int [size];
for ( int i = 0; i < size; i++)
arr2[arr[i] - 1] = i + 1;
for ( int i = 0; i < size; i++)
Console.Write(arr2[i] + " " );
}
public static void Main() {
int []arr = {2, 3, 4, 5, 1};
int size = arr.Length;
inversePermutation(arr, size);
}
}
|
Javascript
function inversePermutation(arr, size) {
let arr2 = [];
for (let i = 0; i < size; i++)
arr2[arr[i] - 1] = i + 1;
for (let i = 0; i < size; i++)
console.log(arr2[i] + " " );
}
let arr = [2, 3, 4, 5, 1];
let size = arr.length;
inversePermutation(arr, size);
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...