#include
using namespace std;
void reverse(int a[],int start, int end){
if(start<end){
if(start==0){
reverse(a,start+1,end);
}
else if(end==0){
reverse(a,start,end-1);
}
else{
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end–;
reverse(a,start,end);
}
}
}
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] << " ";
}
}
Inverse of an array: last test case error. Problem in the code?
don’t confuse inverse with revers
reversing and inversing array are 2 different things
here you have to inverse, not reverse
Inverse is Simply swapping the value of the array with the value at that index.
Algo
- Create a new Array say narr and original array is arr.
- 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.)
- return new array.
you can refer this code
i think you mistakenly shared your code here
i hope your doubt on inverse of array is cleared
so kindly give feedback
if not feel free to ask
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.