Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Java Program to Print the Elements of an Array in Reverse Order

Last Updated on April 25, 2023 by Prepbytes

In this article, we are going to discuss “How to reverse an array in Java”. You are given an array of integers. Your task is to output the array in reverse order. For instance, consider the input array shown below.

So, the output for this array will be the elements in reverse order as shown below.

So, let us now discuss the different approaches to the problem of array reverse in Java.

Reverse an Array by Reverse Print

This approach to the problem of Reverse Array Java is pretty simple. All we need to do is traverse the array in reverse order and print the elements. The approach is shown below.

Initially, we have a pointer at the last element of the array. Now, this pointer will print the current element and move back every step till it reaches -1.

import java.util.*;

public class Main {
    
    public static void reversePrint(int[] arr) {
        for(int i=arr.length-1;i>=0;i--) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        reversePrint(arr);
    }
}

Time Complexity: The time complexity of this method is O(N) as we are just printing the array in reverse and printing an array in reverse requires traversing the entire array of N elements.

Space Complexity (Auxiliary Space): The auxiliary space is O(1) as we have not used any extra space.

Reverse Array Using Auxiliary Array

Now, instead of just printing the array in reverse order, we will reverse the array in the memory i.e. the first element will become the last element and the last element will become the first element, and so on.

This is shown below.

So, the changes are made to the same array. This can be achieved using an auxiliary array. So, we will create an array that is of the same size as that of the input array as shown below.

We can see that we have created an auxiliary array that is of the same size as that of the input array, arr. All the values in the auxiliary array will initially be 0 automatically (property of arrays in Java).

Now, we will iterate through the back of the input array and the beginning of the auxiliary array.

We have a pointer i at the end of the input array and a pointer j at the start of the auxiliary array as shown below.

Now, aux[j] = arr[i] i.e. put the ith element of the input array at the jth index of the auxiliary array and keep incrementing the j pointer and decrementing the ith pointer till i >= 0 && j < aux.length.

Now, we have the reversed array in the auxiliary array as shown below.

Now, we will fill the input array with the values of the corresponding index of the auxiliary array. So, let us say we have a pointer i at index 0.

Now, do arr[i] = aux[i] and keep incrementing the pointer till i < arr.length.

So, the array has been reversed.

Now that we have understood this procedure for the Reverse Array Java problem, let us write the code for the same.

import java.util.*;

public class Main {
    
    public static void reverseUsingAuxArray(int[] arr) {
        int[] aux = new int[arr.length];
        
        int i = arr.length -1;
        int j = 0;
        
        //fill the reverse of arr
        //in aux array
        while(i >= 0) {
            aux[j] = arr[i];
            i--;j++;
        }
        
        //fill back the array arr
        for(i=0;i<arr.length;i++) {
            arr[i] = aux[i];
        }
        
    }
    
    public static void display(int[] arr) {
        for(int i=0;i<arr.length;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        reverseUsingAuxArray(arr);
        display(arr);
    }
}

Time Complexity: The time complexity of this approach is O(N) as we have to traverse the arrays of size N twice.

Space Complexity: The space complexity of this approach is O(N). This is because we have used an auxiliary array of size N.

Reverse Array without Auxiliary Array

We used an auxiliary array to reverse the given array in the previous method. Now, we will reverse our input array without using the auxiliary array.

Consider the array and the pointers i and j as shown below.

So, we have kept the pointer i at the index 0 and the pointer j at arr.length -1 i.e. 5 – 1 = 4.

Now, we will swap the ith value with the jth value as shown below.

Now we will increment the i pointer and decrement the j pointer.

This process will continue till i < j as shown below.

Now, since i is not less than j, we stop.

The array is reversed. So, here we have reversed the array in the memory without using an auxiliary array.

Now that we have understood the procedure, let us write the code for the same.

import java.util.*;

public class Main {
    
    public static void reverse(int[] arr) {
        int i = 0;
        int j = arr.length -1;
        
        while(i < j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;j--;
        }
    }
    
    public static void display(int[] arr) {
        for(int i=0;i<arr.length;i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        reverse(arr);
        display(arr);
    }
}

Time Complexity: The time complexity of this approach is O(N) as we have to traverse the array.

Space Complexity (Auxiliary Space): The auxiliary space used is O(1) as we have not used any extra space.

Reverse Array Using Collections.reverse() Method

In Java, the method reverse() in the Class Collections is used to reverse collections like List, ArrayList, LinkedList, etc. So, the array can be reversed using this method by first converting it into a List and then using the reverse() method of the Collections class.

The program to do the same is shown below.

import java.util.*;
public class Main {   
    public static void reverse(Integer[] arr) {
        Collections.reverse(Arrays.asList(arr));
        System.out.println(Arrays.asList(arr));
    }   
    public static void main(String[] args) {
        Integer[] arr = {1,2,3,4,5};
        reverse(arr);
    }
}

Time Complexity: The time complexity of this approach is O(N) as internally, the array will first be converted into list and all the elements will be traversed.

Space Complexity (Auxiliary Space): The auxiliary space is O(N) as internally, Java will use an auxiliary list and reverse it.

So, we have now solved the Reverse Array Java problem using 4 different methods. We hope that you have understood all the 4 methods and enjoyed the discussion. See you again at PrepBytes.
You can also checkout our post on:
How to Check Given Number is Armstrong or not
20 Must Practice basic Java Programs for beginners
Bubble Sort in Java

Leave a Reply

Your email address will not be published. Required fields are marked *