Reverse an Array | Practice | GeeksforGeeks

Given an array A of size N, print the reverse of it.
Input:
First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array.
Output:
For each testcase, in a new line, print the array in reverse order. 
Constraints:
1 <= T <= 100
1 <= N <=100
0 <= Ai <= 100
Example:
Input:
1
4
1 2 3 4
Output:
4 3 2 1
0
shubham7905521 day ago
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args){   Scanner sc=new Scanner(System.in);int t= sc.nextInt();while(t>0){   int n=sc.nextInt();   int arr[]=new int [n];      for(int i=0;i<n;i++)   {       arr[i]=sc.nextInt();   }     reverse(arr,0,n-1);   for(int i = 0; i < n; i++)   {          System.out.print(arr[i] + " ");   }      System.out.println();      t--;}}public static void reverse (int array[],int start, int end){        while (start<end){                array[start]=array[start]+array[end]-(array[end]=array[start]);        start++;        end--;    }    }}
0
chandu0987654321c2 days ago
 
T =int(input())while T>0:   num = int(input())   A = []   n1 = input()   A = list(map(int,n1.split(" ")))   for i in range(num-1,-1,-1):       print(A[i],end = " ")   T-=1
0
venkatasaikrishna14883 days ago
any body please reply why my code is not working it runs in local editors and gives correct output
class GFG {public static void main (String[] args) { Scanner s = new Scanner(System.in); int testCases = s.nextInt(); for(int i=0;i< testCases;i++){ int n = s.nextInt(); int[] arr = new int[n]; for(int a=0;a<n;a++){     arr[a] = s.nextInt(); } int[] revArr = new int[n]; int k=0; for(int j=n-1;j>=0;j--){     revArr[k] = arr[j];     k++; }  for(int x=0;x<n;x++){     System.out.print(revArr[x]+" "); } }}}
0
meghakonnur8153 days ago
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args) { Scanner sc=new Scanner(System.in);int t= sc.nextInt();while(t>0){    int n=sc.nextInt();    int arr[]=new int [n];        for(int i=0;i<n;i++){        arr[i]=sc.nextInt();    }      reverse(arr,0,n-1);    for(int i = 0; i < n; i++) {           System.out.print(arr[i] + " ");       }       System.out.println();       t--;} }  public static void reverse (int array[],int start, int end){          while (start<end){                  array[start]=array[start]^array[end];         array[end]=array[start]^array[end];         array[start]=array[start]^array[end];         start++;         end--;     }      }}
+2
harshscode4 days ago
 int t;  cin>>t;  while(t--)  {      int n;      cin>>n;      int a[n];      for(int i=0;i<n;i++)      cin>>a[i];            for(int i=n-1;i>=0;i--)      cout<<a[i]<<" ";      cout<<endl;  }return 0;
0
wwwpradeepmba4 days ago
import java.util.*;import java.lang.*;import java.io.*;class GFG{public static void main (String[] args) { Scanner sc=new Scanner(System.in);int t= sc.nextInt();while(t>0){    int n=sc.nextInt();    int arr[]=new int [n];        for(int i=0;i<n;i++){        arr[i]=sc.nextInt();    }      reverse(arr,0,n-1);    for(int i = 0; i < n; i++) {           System.out.print(arr[i] + " ");       }       System.out.println();       t--;} }  public static void reverse (int array[],int start, int end){          while (start<end){                  array[start]=array[start]^array[end];         array[end]=array[start]^array[end];         array[start]=array[start]^array[end];         start++;         end--;     }      }}
0
aravindmn75835 days ago
//simple c++ two pointer solution;-→
#include<bits/stdc++.h>
using namespace std;
void reverse(int a[],int n){
    int i=0;
    int j=n-1;
    while(i<j){
        swap(a[i],a[j]);
        i++;
        j--;
    }
}

int main(){
    int tc;
    cin>>tc;
    while(tc){
        int n;
        cin>>n;
        int a[n];
        for(int i=0; i<n; i++){
            cin>>a[i];
        }
        reverse(a,n);
        for(int i=0; i<n; i++){
            cout<<a[i]<<" ";
        }
        cout<<endl;
        tc--;
    }
}
0
anmoldon6496 days ago
n=int(input())
for i in range(n):
    l=[map(int,input().split())]
    l.reverse()
    print(l)
0
bhushanaditya711 week ago
#include <bits/stdc++.h>
using namespace std;
int main() {
//code
int testCases = 0;
   cin>>testCases;
   
   while(testCases>0)
   {
       int arrLen = 0;
       cin>> arrLen;
       int arr[arrLen];
       
       for(int i=0; i<arrLen; i++)
       {
           cin>>arr[i];
       }
       
       for(int i=arrLen-1; i>=0; i--)
       {
           cout<<arr[i]<<" ";
       }
       
       cout<<endl;
       testCases--;
   }
return 0;
}
0
toppamanv
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.