-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInverse_Permutation.cpp
More file actions
56 lines (50 loc) · 961 Bytes
/
Inverse_Permutation.cpp
File metadata and controls
56 lines (50 loc) · 961 Bytes
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
/*
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
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.
*/
#include <iostream>
using namespace std;
void printReverse(int a[],int n){
int a2[n];
for(int i = 0; i<n; i++){
a2[a[i]-1] = i+1;
}
for(int i = 0; i<n; i++){
cout<<a2[i]<<" ";
}
cout<<endl;
}
int main() {
//code
int t;
cin>>t;
while(t-- > 0){
int n;
cin>>n;
int a[n];
for(int i = 0; i<n; i++){
cin>>a[i];
}
printReverse(a,n);
}
return 0;
}