Whats wrong in the code

#include<bits/stdc++.h>
using namespace std;

void reverse(int *a,int i,int j){
if(i>j){
return;
}
swap(a[i],a[j]);
return reverse(a,i+1,j-1);
}

int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
reverse(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}

return 0;

}

hi @dhruvj797
Inverse is Simply swapping the value of the array with the value at that index.

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<iostream>
#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;
}

hi @dhruvj797
I hope it’s clear now???

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.