Do following for every element arr[i] While index[i] is not equal to i (i) Store array and index values of the target (or correct) position where arr[i] should be placed. The correct position for arr[i] is index[i] (ii) Place arr[i] at its correct position. Also update index value of correct position. (iii) Copy old values of correct position (Stored in step (i)) to arr[i] and index[i] as the while loop continues for i.
//https://www.geeksforgeeks.org/reorder-a-array-according-to-given-indexes/
#include <iostream>
using namespace std;
int main() {
int a[] = {50, 40, 70, 60, 90};
int index[] = {3, 0, 4, 1, 2};
int n=5;
int t=0;
for (int i=0;i<n;i++) {
while (index[i]!=i) {
int temp1= index[index[i]];
int temp2= a[index[i]];
a[index[i]]= a[i];
index[index[i]]= index[i];
index[i]= temp1;
a[i]= temp2;
}
}
for (int i=0;i<n;i++)
cout<< index[i]<< " "<< a[i]<< endl;
return 0;
}