# Reverse an Array in Java

In this tutorial, we will discuss how one can reverse an array in Java. In the input, an integer array is given, and the task is to reverse the input array. Reversing an array means the last element of the input array should be the first element of the reversed array, the second last element of the input array should be the second element of the reversed array, and so on. Observe the following examples.

Example 1:

Input:

arr[] = {1, 2, 3, 4, 5, 6, 7, 8}

Output

arr[] = {8, 7, 6, 5, 4, 3, 2, 1}

Example 2:

Input:

arr[] = {4, 8, 3, 9, 0, 1}

Output:

arr[] = {1, 0, 9, 3, 8, 4}

## Approach 1: Using an auxiliary array

We can traverse the array from end to beginning, i.e., in reverse order, and store the element pointed by the loop index in the auxiliary array. The auxiliary array now contains the elements of the input array in reversed order. After that, we can display the auxiliary array on the console. See the following program.

FileName: ReverseArr.java

Output:

```For the input array:
1 2 3 4 5 6 7 8
The reversed array is:
8 7 6 5 4 3 2 1

For the input array:
4 8 3 9 0 1
The reversed array is:
1 0 9 3 8 4
```

Complexity Analysis: A for loop is required to reverse the array, which makes the time complexity of the program O(n). Also, an auxiliary array is required to reverse the array making the space complexity of the program O(n), where n is the total number of elements present in the array.

## Approach 2: Using Two Pointers

We can also use two pointers to reverse the input array. The first pointer will go to the first element of the array. The second pointer will point to the last element of the input array. Now we will start swapping elements pointed by these two pointers. After swapping, the second pointer will move in the leftward direction, and the first pointer will move in the rightward direction. When these two pointers meet or cross each other, we stop the swapping, and the array we get is the reversed array of the input array.

FileName: ReverseArr1.java

Output:

```For the input array:
1 2 3 4 5 6 7 8
The reversed array is:
8 7 6 5 4 3 2 1

For the input array:
4 8 3 9 0 1
The reversed array is:
1 0 9 3 8 4
```

Complexity Analysis: The time complexity of the program is the same as the previous program. There is no extra space used in the program, making the space complexity of the program O(1).

## Approach 3: Using Stack

Since a Stack works on the LIFO (Last In First Out) principle, it can be used to reverse the input array. All we have to do is to put all the elements of the input array in the stack, starting from left to right. We will do it using a loop.

FileName: ReverseArr2.java

Output:

```For the input array:
1 2 3 4 5 6 7 8
The reversed array is:
8 7 6 5 4 3 2 1

For the input array:
4 8 3 9 0 1
The reversed array is:
1 0 9 3 8 4
```

Complexity Analysis: The time complexity of the program is the same as the previous program. There is stack used in the program, making the space complexity of the program O(n).

### Using Recursion

Using recursion also, we can achieve the same result. Observe the following.

FileName: ReverseArr3.java

Output:

```For the input array:
1 2 3 4 5 6 7 8
The reversed array is:
8 7 6 5 4 3 2 1

For the input array:
4 8 3 9 0 1
The reversed array is:
1 0 9 3 8 4
```

Explanation: The statement reverseArr.add(arr[i]); is written after the recursive call goes in the stack (note that the stack is implicit in this case). So, when the base case is hit in the recursive call, stack unwinding happens, and whatever is there in the stack pops out. The last element goes into the stack during the last recursive call. Therefore, the last element is popped out first. Then the penultimate element is popped out, and so on. The statement reverseArr.add(arr[i]); stores that popped element. In the end, we are displaying the elements that are stored in the list reverseArr.

Complexity Analysis: Same as the first program of approach-3.

## Approach 4: Using Collections.reverse() method

The build method Collections.reverse() can be used to reverse the list. The use of it is shown in the following program.

FileName: ReverseArr4.java

Output:

```For the input array:
1 2 3 4 5 6 7 8
The reversed array is:
8 7 6 5 4 3 2 1

For the input array:
4 8 3 9 0 1
The reversed array is:
1 0 9 3 8 4
```

Complexity Analysis: The program uses Collections.reverse() method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.

## Approach 5: Using StringBuilder.append() method

It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.

FileName: ReverseArr5.java

Output:

```For the input array:
javaTpoint is the best website
The reversed array is:
website best the is javaTpoint

For the input array:
India is my country
The reversed array is:
country my is India
```

Complexity Analysis: The time and space complexity of the program is the same as the previous program.