-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInverse Permutation
77 lines (61 loc) · 2.13 KB
/
Inverse Permutation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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).
Input:
The first line of the input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer n, denoting the size of the array A followed by n-space separated integers i.e elements of array A.
Output:
For each test case, the output is the array after performing inverse permutation on A.
Constraints:
1<=T<=100
1<=n<=50
1<=A[i]<=50
Note: Array should contain unique elements and should have elements from 1 to n.
Example:
Input:
3
4
1 4 3 2
5
2 3 4 5 1
5
2 3 1 5 4
Output:
1 4 3 2
5 1 2 3 4
3 1 2 5 4
Explanation:
1. 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.
2. 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.
CODE:
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner in =new Scanner(System.in);
int t=in.nextInt();
while(t--!=0)
{
int n=in.nextInt();
int temp;
int arr1[]=new int[n];
int arr2[]=new int[n];
for(int i=0;i<n;i++)
{
arr1[i]=in.nextInt();
}
for(int j=0;j<n;j++)
{
temp=arr1[j];
arr2[temp-1]=j+1;
}
for(int k=0;k<n;k++)
{
System.out.print(arr2[k]+" ");
}
System.out.println("");
}
}
}