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++
// 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;
}
Java
// 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
Python3
# 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 contributed by Smitha Dinesh Semwal
C#
// 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
PHP
<?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
?>
JavaScript
<script>
// Naive JavaScript program to find inverse permutation.
// JavaScript function to find inverse permutations
function inversePermutation(arr, size)
{
let 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
document.write( j + 1 + " ");
break;
}
}
}
}
// Driver code
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++
// 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;
}
// The code is contributed by Nidhi goel.
Java
// 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.
Python3
# 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.
C#
// 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.
JavaScript
// function to find inverse permutations
function inversePermutation(arr, size) {
// to store element to index mappings
let arr2 = [];
// Inserting position at their
// respective element in second array
for (let i = 0; i < size; i++)
arr2[arr[i] - 1] = i + 1;
for (let i = 0; i < size; i++)
console.log(arr2[i] + " ");
}
// Driver program to test above function
let arr = [2, 3, 4, 5, 1];
let size = arr.length;
inversePermutation(arr, size);
// This code is contributed by aadityaburujwale.
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Number of permutation with K inversions
We are given two numbers n and k, the task is to find how many permutations of the first n number have exactly k inversion. Two elements in a sequence form an inversion if the smaller element appears before the larger element.Examples: Input: n = 3, k = 1Output: 2Explanation: Total Permutation of fi
15 min read
CSES Solutions - Permutation Inversions
Your task is to count the number of permutations of 1, 2, ⦠n that have exactly k inversions (i.e., pairs of elements in the wrong order). Examples: Input: n = 4, k = 3Output: 6Explanation: For n=4 and k=3, there are 6 permutations that satisfy the given conditions. The permutations are: [1, 4, 3, 2
4 min read
Modular multiplicative inverse
Given two integers A and M, find the modular multiplicative inverse of A under modulo M.The modular multiplicative inverse is an integer X such that:A X â¡ 1 (mod M) Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 m
15+ min read
Number of permutation with K inversions | Set 2
Given two integers N and K, the task is to count the number of permutations of the first N natural numbers having exactly K inversions. Since the count can be very large, print it modulo 109 + 7. An inversion is defined as a pair a[i], a[j] such that a[i] > a[j] and i < j. Examples: Input: N =
8 min read
Number of Transpositions in a Permutation
Cycle notation is a compact way to represent a permutation by breaking it down into cycles. A cycle represents a set of elements that are permuted (or swapped) among each other.Examples:Let us consider the permutation p = [5, 1, 4, 2, 3] of [1, 2, 3, 4, 5], the elements are moved as 1 â 5, 5 â 3, 3
6 min read
Adjoint and Inverse of a Matrix
Given a square matrix, find the adjoint and inverse of the matrix. We strongly recommend you to refer determinant of matrix as a prerequisite for this. Adjoint (or Adjugate) of a matrix is the matrix obtained by taking the transpose of the cofactor matrix of a given square matrix is called its Adjoi
15+ min read
Modular multiplicative inverse from 1 to n
Give a positive integer n, find modular multiplicative inverse of all integer from 1 to n with respect to a big prime number, say, 'prime'. The modular multiplicative inverse of a is an integer 'x' such that. a x ? 1 (mod prime) Examples: Input : n = 10, prime = 17 Output : 1 9 6 13 7 3 5 15 2 12 Ex
9 min read
PHP | gmp_invert() for inverse modulo
The gmp_invert() is a built-in function in PHP which is used to find the modular inverse of a GMP number (GNU Multiple Precision : For large numbers) under another GMP number. The modular inverse is a number x such that: a x ≡ 1 (mod b) The value of x should be in {0, 1, 2, ⦠b-1}, i.e., in th
3 min read
Find a permutation of N natural numbers with K inversions
Given two integers N and K, the task is to find a permutation of first N natural numbers with exactly K inversions. Examples : Input: N = 5, K = 4Output: 5 1 2 3 4 Explanation: In the above permutation P, the pairs (i, j) such that i < j and P[i] > P[j] are (0, 1), (0, 2), (0, 3), and (0, 4).
9 min read
Check if the given array is same as its inverse permutation
Given an array arr[] consisting of integers in the range [1, N], the task is to determine whether the Inverse Permutation of the given array is same as the given array. An inverse permutation is a permutation obtained by inserting the position of all elements at the position equal to the respective
8 min read