Iterative Approach to Reverse an Array

Problem Statement

You’re given an array arr. You need to reverse the elements of the array, then print the reversed array. You need to implement this solution using loops.

Example 1: Let arr = [45, 12, 67, 63, 9, 23, 74]

Reversed arr = [74, 23, 9, 63, 67, 12, 45]

Thus the output is: 74 23 9 63 67 12 45.

Example 2: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]

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

Thus the output is: 8 7 6 5 4 3 2 1.

Approach to Reverse an Array Using Loops

You can reverse the elements of an array using loops by following the approach below:

Initialize index variables “i” and “j” such that they point to the first (0) and the last (sizeOfArray - 1) index of the array respectively. In a loop, swap the element at index i with the element at index j. Increment the value of i by 1 and decrement the value of j by 1. Run the loop until i < sizeOfArray/2.

C++ Program to Reverse an Array Using Loops

Below is the C++ program to reverse an array using loops:

Output:

Python Program to Reverse an Array Using Loops

Below is the Python program to reverse an array using loops:

Output:

JavaScript Program to Reverse an Array Using Loops

Below is the JavaScript program to reverse an array using loops:

Output:

Recursive Approach to Reverse an Array

Problem Statement

You’re given an array arr. You need to reverse the elements of the array, then print the reversed array. You need to implement this solution using recursion.

Example 1: Let arr = [45, 12, 67, 63, 9, 23, 74]

Reversed arr = [74, 23, 9, 63, 67, 12, 45]

Thus the output is 74 23 9 63 67 12 45.

Example 2: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]

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

Thus the output is 8 7 6 5 4 3 2 1.

Approach to Reverse an Array Using Recursion

You can reverse the elements of an array using recursion by following the approach below:

Initialize index variables start and end such that they point to the first (0) and the last (sizeOfArray - 1) index of the array respectively. Swap the element at the index start with the element at the index end. Recursively call the reverse function. In parameters of the reverse function, increment the value of start by 1 and decrement the value of end by 1. Stop the recursion when the value of the start variable is greater than or equal to the value of the end variable.

C++ Program to Reverse an Array Using Recursion

Below is the C++ program to reverse an array using recursion:

Output:

Python Program to Reverse an Array Using Recursion

Below is the Python program to reverse an array using recursion:

Output:

JavaScript Program to Reverse an Array Using Recursion

Below is the JavaScript program to reverse an array using recursion:

Output:

Use Recursion to Solve Problems

A recursive function is a function that calls itself. In recursion, a problem is solved by breaking down the problems into smaller, simpler versions of themselves.

There are many advantages of recursion: the recursive code is shorter than an iterative code, it can be used to solve the problems that are naturally recursive, it can be used in infix, prefix, postfix evaluations, recursion reduces the time needed to write and debug code, etc.

Recursion is a favorite topic of interviewers in technical interviews. You must know about recursion and how to use it while writing code to be the most efficient programmer you can be.