**Reverse an Array Using Inbuilt Methods**

Reversing an array using inbuilt methods involves using predefined functions or methods provided by the programming language to reverse the elements of the array.

**Implementation**

### C++

`#include <iostream>`

#include <algorithm>

using namespace std;

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

reverse(arr, arr + n);

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

}

return 0;

}

### Java

`import java.util.Arrays;`

class Main {

public static void main(String[] args) {

int arr[] = {1, 2, 3, 4, 5};

Arrays.sort(arr);

for (int i = arr.length - 1; i >= 0; i--) {

System.out.print(arr[i] + " ");

}

}

}

### Python

`arr = [1, 2, 3, 4, 5]`

arr.reverse()

print(arr)

**Output**

```
5 4 3 2 1
```

**Time Complexity**

The time complexity for reversing an array using inbuilt methods varies depending on the implementation, but it is typically O(n).

**Space Complexity**

The space complexity depends on the implementation, but it is usually O(1) or O(n) if additional space is required for the method.

**Reverse an Array Using Recursion**

Reversing an array using recursion involves defining a recursive function to reverse a subarray and then swapping the elements recursively.

**Implementation**

### C++

`#include <iostream>`

using namespace std;

void reverseArray(int arr[], int start, int end) {

if (start >= end)

return;

swap(arr[start], arr[end]);

reverseArray(arr, start + 1, end - 1);

}

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

reverseArray(arr, 0, n - 1);

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

}

return 0;

}

### Java

`class Main {`

static void reverseArray(int arr[], int start, int end) {

if (start >= end)

return;

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

reverseArray(arr, start + 1, end - 1);

}

public static void main(String[] args) {

int arr[] = {1, 2, 3, 4, 5};

reverseArray(arr, 0, arr.length - 1);

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

}

}

### Python

`def reverse_array(arr, start, end):`

if start >= end:

return

arr[start], arr[end] = arr[end], arr[start]

reverse_array(arr, start + 1, end - 1)

arr = [1, 2, 3, 4, 5]

reverse_array(arr, 0, len(arr) - 1)

print(arr)

**Output**

```
5 4 3 2 1
```

**Time Complexity**

The time complexity for reversing an array using recursion is O(n), where n is the number of elements in the array.

**Space Complexity**

The space complexity for the recursive implementation is O(n) due to the recursive calls made on the call stack.

**Reverse an Array Using Stack**

Reversing an array using a stack involves pushing the elements of the array onto a stack and then popping them off the stack to reverse the order.

**Implementation**

### C++

`#include <iostream>`

#include <stack>

using namespace std;

void reverseArray(int arr[], int n) {

stack<int> s;

for (int i = 0; i < n; i++) {

s.push(arr[i]);

}

for (int i = 0; i < n; i++) {

arr[i] = s.top();

s.pop();

}

}

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

reverseArray(arr, n);

for (int i = 0; i < n; i++) {

cout << arr[i] << " ";

}

return 0;

}

### Java

`import java.util.Stack;`

class Main {

static void reverseArray(int arr[]) {

Stack<Integer> stack = new Stack<>();

for (int i = 0; i < arr.length; i++) {

stack.push(arr[i]);

}

for (int i = 0; i < arr.length; i++) {

arr[i] = stack.pop();

}

}

public static void main(String[] args) {

int arr[] = {1, 2, 3, 4, 5};

reverseArray(arr);

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

}

}

### Python

`def reverse_array(arr):`

stack = []

for num in arr:

stack.append(num)

for i in range(len(arr)):

arr[i] = stack.pop()

arr = [1, 2, 3, 4, 5]

reverse_array(arr)

print(arr)

**Output**

```
5 4 3 2 1
```

**Time Complexity**

The time complexity for reversing an array using a stack is O(n), where n is the number of elements in the array.

**Space Complexity**

The space complexity is O(n) as it requires extra space for the stack to store the elements.

**Frequently Asked Questions**

**How to reverse order in array in C?**

To reverse the order of elements in an array in C, iterate through the array from both ends and swap elements until reaching the middle of the array.

**How do you reverse an array in C++ using function?**

In C++, you can reverse an array using the **reverse()** function from the **<algorithm>** header. Pass the beginning and end iterators of the array to the function to reverse its elements.

**How to reverse a 2D array in C?**

To reverse a 2D array in C, iterate through each row of the array and reverse the order of elements within each row using the same technique as reversing a 1D array.

**Can I reverse an array in less than O(N) time complexity?**

No, you can’t do that because for reversing an array, you need to travel all the array elements in one traversal time complexity will be O(N).

## Conclusion

In this article, you learned how to reverse an array using various approaches like recursion, stack, loop, etc. An array is the most asked data structure in interviews, so it’s mandatory to learn about different operations you can perform on an array.

We also discussed the time and space complexity of reversing an array. You should always try to find the space and time complexity of any program you write, as in the real world, we have to calculate in advance the time and space our program will take.

**Recommended problems - **

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and Algorithms, Competitive Programming, Aptitude, and many more!

Give your career an edge over others by considering our premium courses!