Inverse Permutation | Practice | GeeksforGeeks

Given an array A of size n of integers in the range from 1 to n, we need to find the inverse permutation of that array.
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).
 
Example 1:
Input:
N = 4
Arr[] = {1, 4, 3, 2}
Output:
1 4 3 2
Explanation:
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:
N = 5
Arr[] = {2, 3, 4, 5, 1}
Output:
5 1 2 3 4
Explanation:
As index 1 has value 2 so 1 will b 
placed at index 2, similarly 2 has
3 so 2 will be placed at index 3
similarly 3 has 4 so placed at 4,
4 has 5 so 4 placed at 5 and 5 
has 1 so placed at index 1. 
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function inversePermutation() which takes the array Arr[] and its size N as inputs and returns the vector array after performing inverse permutation on Arr.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ N
Note: Array should contain unique elements and should
  have all elements from 1 to N. 
0
yesbro3 weeks ago
public static ArrayList<Integer> inversePermutation (int arr[], int n) {
        //Complete the function
        int index = 0;
        int value = 0;
        int[] a = new int[n];
        ArrayList<Integer> list = new ArrayList<Integer>(n);
        for(int i=0;i<n;i++){
            index = arr[i];
            value = i;
            a[index-1] = value+1;
        }
        for(int i=0;i<n;i++){
            list.add(a[i]);
        }
        return list;
    }
0
priyaranjandash1 month ago
******java*****
class Complete{        // Function for finding maximum and value pair   public static ArrayList<Integer> inversePermutation (int arr[], int n) {        ArrayList<Integer> list = new ArrayList<Integer>(n);    int b[] = new int[n];    for(int i=0;i<n;i++) {     int value = arr[i];     b[value-1] = i+1;        }        for(int i=0;i<n;i++) {    list.add(b[i]);   }    return list;   }      }
0
dangrio2 months ago
 vector<int> ansVec(size);   for(int i=0;i<size;i++){       ansVec[arr[i]-1] = i+1;   }   return ansVec;
-1
vishwajeetofficial20222 months ago
//JAVA SOLUTION

class Complete{
    
   
    // Function for finding maximum and value pair
    public static ArrayList<Integer> inversePermutation (int arr[], int n) {
        //Complete the function
         ArrayList<Integer> aL = new ArrayList<Integer>();
       int [] tempArr;
       
       if (arr.length > 0 && n > 0) {
           
          tempArr = new int[arr.length];
           
           for (int i =0; i < arr.length; i++) {
               
               tempArr[arr[i]-1] = i+1;
           }
           
           for (int temp : tempArr)
           aL.add(temp);
       }
       
       return aL;
    }
    
    
}



0
paurushbatish3 months ago
vector<int> inversePermutation(int arr[], int size) {
    vector<int>v(size,0);
    for(int i = 0;i<size;i++){
        v[arr[i]-1] = i+1;
    }
    return v;
}
0
chrisfaraglia4 months ago
class Complete{
   
   public static ArrayList<Integer> inversePermutation (int arr[], int n) {
       
       ArrayList<Integer> inverseArr = new ArrayList<Integer>();
       int [] tempArr;
       
       if (arr.length > 0 && n > 0) {
           
          tempArr = new int[arr.length];
           
           for (int i =0; i < arr.length; i++) {
               
               tempArr[arr[i]-1] = i+1;
           }
           
           for (int temp : tempArr)
           inverseArr.add(temp);
       }
       
       return inverseArr;
   }
}
0
patelt92384 months ago
// why is it not working 
vector<int> inversePermutation(int arr[], int size) {   vector<int> v;   int a[size];   for(int i=0;i<size;i++){       a[arr[i]]=i+1;   }   for(int i=0;i<size;i++){   v.push_back(a[i]);   }   return v;}
+2
priyankarajpoot2015 months ago
Java Solution-
class Complete{   // Function for finding maximum and value pair   public static ArrayList<Integer> inversePermutation (int arr[], int n) {       ArrayList<Integer> list = new ArrayList<>();       int[] a = new int[n+1];       for(int i=0;i<n;i++){           a[arr[i]] = i+1;       }       for(int i=1;i<=n;i++){           list.add(a[i]);       }       return list;   }}
+1
badgujarsachin836 months ago
vector<int> inversePermutation(int arr[], int size) {
    vector<int> v;
    map<int,int> mp;
    for(int i=0;i<size;i++){
        mp[arr[i]]=i+1;
    }
    for(auto it:mp){
        v.push_back(it.second);
    }
    return v;
}
-9
rohitvishwakarma18197 months ago
successfully tried it without using extra space. But used it to match the return type 😉
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.