Don't know what to swap

My code:

#include
using namespace std;
void inversearray(int a[],int n){
for(int i=0;i<n;i++){
int temp=a[i];
a[temp]=i;
i=temp;
}
}
void printarray(int a[],int n){
for(int i=0;i<n;i++){
cout<<a[i]<<’ ';
}
}
int main(){
int a[100],end,start,n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
end=n-1;
start=0;
inversearray(a,n);
printarray(a,n);
return 0;
}

Please can you tell what’s wrong with my code

hi @sakshichauhan9678_11c372704eb261e1,
Algo

  1. Create a new Array say narr and original array is arr.
  2. now at the arr[i]th index of narr save the ith value.(previously i is the index and arr[i] is the value bt now arr[i] is the index and i is the value.)
  3. return new array.

refer this code ->

#include<bits/stdc++.h>
using namespace std;
int main() {
   int N;
   cin>>N;
   int a[N];
   for(int i =0;i<N;i++)
   	cin>>a[i];
   int b[N];
   for(int j=0;j<N;j++){
   	b[a[j]]=j;
   }
   for(int i=0;i<N;i++){
       cout<<b[i]<<" ";
   }
   return 0;
}
1 Like