Print Array in Reverse Order
Now that we've seen how to traverse an array in a forward manner, there are many situations where we need to traverse and perform operations on an array in reverse order as well. Let's explore how this is done with the below problem.
Write a program that takes an array as input and prints each element of the array in reverse order.

Example
Input: nums = [1, 2, 3, 5, 6]
Output: [6, 5, 3, 2, 1]
Input: nums = [1, 2, 9, 5, 6, 7, 8, 2, 3]
Output: [3, 2, 8, 7, 6, 5, 9, 2, 1]
Approach
To solve this problem, we use a loop that starts from the end of the array and moves toward the beginning. Instead of looping from 0 to n - 1, which is used when traversing the array in a forward manner (where n is the size of the array), we start from n - 1 and decrement the loop variable in each step. This way, we access and print each element from the last to the first, resulting in the array being printed in reverse order.


Dry Run
Input:
nums = [1, 2, 3, 5, 6]
size = 5
Output:
[6, 5, 3, 2, 1]
- Initialization:
- The input array
nums
is [1, 2, 3, 5, 6]. - The size of the array is 5.
- The input array
- Iterations of the Loop:
- First Iteration (index = 4):
- The value at nums[4] is 6.
- This value is printed, resulting in partial output: 6
- Second Iteration (index = 3):
- The value at nums[3] is 5.
- This value is printed, resulting in partial output: 6 5
- Third Iteration (index = 2):
- The value at nums[2] is 3.
- This value is printed, resulting in partial output: 6 5 3
- Fourth Iteration (index = 1):
- The value at nums[1] is 2.
- This value is printed, resulting in partial output: 6 5 3 2
- Fifth Iteration (index = 0):
- The value at nums[0] is 1.
- This value is printed, resulting in partial output: 6 5 3 2 1
- End of Loop: The loop terminates when index = -1, which is less than 0.
Code for All Languages
C++
// Function to print each element of the array in reverse order
void printArrayInReverse(int nums[], int size) {
for (int index = size - 1; index >= 0; --index) {
cout << nums[index] << " ";
}
cout << endl; // Newline for better output formatting
}
Java
// Function to print each element of the array in reverse order
public static void printArrayInReverse(int[] nums, int size) {
for (int index = size - 1; index >= 0; --index) {
System.out.print(nums[index] + " ");
}
System.out.println(); // Newline for better output formatting
}
Python
# Function to print each element of the array in reverse order
def print_array_in_reverse(nums, size):
for index in range(size - 1, -1, -1):
print(nums[index], end=" ")
print() # Newline for better output formatting
Time Complexity : O(n)
The time complexity of this program is O(n), where n is the number of elements in the array. This is because the program iterates through the array once, but in reverse order. Whether iterating forward or backward, the loop still runs n times, performing a constant-time operation (printing) for each element. Therefore, the time complexity remains linear, O(n).
Space Complexity : O(1)
Auxiliary Space Complexity
This refers to any extra space used by the algorithm that is independent of the input size.
In this case, the only additional space used is for variables such as index and size, which take up constant space. These variables do not grow with the size of the array, so the auxiliary space complexity is O(1).
Total Space Complexity
This is the space required for both the input array and any auxiliary space used by the algorithm.
The input array nums[] is of size n, provided by the user. No additional arrays or data structures are created inside the function.
So, the total space complexity is O(n).