Home Programming Reverse an Array

Reverse an Array

by anupmaurya
1 minutes read

Problem: Given an array A of size N, print the reverse of it.

Example:

Input:
4  // size of array
1 2 3 4  //array element
Output:
4 3 2 1  //reverse of array

Program to print reverse an Array

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

int main() {
//code
int t,n,temp,i,j;
cin>>t;
for(int i=0;i<t;i++){
    cin>>n;
    vector<int>arr(n);
    for(int i=0; i<n; i++){
       cin>>arr[i];
    }
    i=0;
    j=n-1;
    while(i<=j)
    {   
        temp=arr[j];
        arr[j]=arr[i];
        arr[i]=temp;
        i++;
        j--;
    }
    
    for(auto it1 : arr) 

       cout<<it1<<" ";
        cout<<endl;
}
     return 0;
}

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.