Skip to main content

Array Basics

Print Array in Reverse Order Solution in C++/Java/Python/JS

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.

reverse-array-print
reverse-array-print
reverse-array-print
reverse-array-print

Dry Run

Input:
nums = [1, 2, 3, 5, 6]
size = 5

Output:
[6, 5, 3, 2, 1]

  1. Initialization:
    • The input array nums is [1, 2, 3, 5, 6].
    • The size of the array is 5.
  2. 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
  3. 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

Javascript
// Function to print each element of the array in reverse order
function printArrayInReverse(nums, size) {
    for (let index = size - 1; index >= 0; --index) {
        process.stdout.write(nums[index] + " ");
    }
    console.log(); // 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 space and output space. In this case, we only use a constant amount of extra space, specifically for the variables. These variables do not depend on the size of the array and therefore take up constant space. so the auxiliary space complexity is O(1).

Total Space Complexity: This includes the space required for the input, output and extra space used by the algorithm as well. The input array nums[] is of size n, So the space required for input space is O(n). No output space is used. Also, the algorithm takes only constant extra space.
Total Space Complexity = O(n) + O(1) + O(1) = O(n)

Join Our Course

dsa banner

Offer ends in:

0

Days

0

Hours

0

Mins

0

Secs

LearnYard Technologies FZ-LLC
Code, Create, Conquer.