Open In App

C Program to Traverse an Array in Reverse

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C program to traverse a given array in reverse order that contains N elements.

Examples

Input: arr[] = {2, -1, 5, 6, 0, -3}
Output: -3 0 6 5 -1 2

Input: arr[] = {4, 0, -2, -9, -7, 1}
Output: 1 -7 -9 -2 0 4

Different Ways to Traverse an Array in Reverse Order in C

We can traverse/print the array in the reverse direction using the following different methods in C:

1. Using a Loop

The most straightforward method to traverse an array in reverse is by using a loop. This involves iterating from the last index (N - 1) to the first index (0).

C Program to Traverse an Array in Reverse Order Using a Loop

C
// C Program to Traverse an Array in Reverse Order Using a Loop
#include <stdio.h>

int main() {
    int arr[] = {2, -1, 5, 6, 0, -3};
    int N = sizeof(arr) / sizeof(arr[0]);

    // Loop that goes from N - 1 to 0
    for (int i = N - 1; i >= 0; i--)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output
-3 0 6 5 -1 2 

Time Complexity: O(N)
Auxiliary Space: O(1)

2. Using Recursion

Recursion can also be used to traverse an array in reverse order. It is less efficient than looping due to the overhead of function calls and additional memory usage.

Below is the approach to use recursion to traverse the array in revese:

  • Define a recursive function that takes the array and the current index as arguments.
  • The base case is when all elements are traversed.
  • Recursively call the function for the next element until the first is reached.
  • Print the current element after the recursive call.

C Program to Traverse an Array in Reverse Order Using Recursion

C
// C Program to Traverse an Array in Reverse Order Using Recursion
#include <stdio.h>

void traverseReverseRecursive(int arr[], int N) {
    if (N <= 0) {
        return;
    }

    // Print the current element after recursive call
    printf("%d ", arr[N - 1]);
    traverseReverseRecursive(arr, N - 1);
}

int main() {
    int arr[] = {2, -1, 5, 6, 0, -3};
    int N = sizeof(arr) / sizeof(arr[0]);

    // Traverse and print the array in reverse order using recursion
    traverseReverseRecursive(arr, N);

    return 0;
}

Output
-3 0 6 5 -1 2 

Time Complexity: O(N)
Auxiliary Space: O(N), due to the recursive stack usage.


Suggested Quiz
5 Questions
What is the advantage of using recursion to traverse an array in reverse order?
  • A
    Fewer lines of code
  • B
    Easier to implement
  • C
    More readable code
  • D
    All of the above
Explanation:
Which of the following is a disadvantage of the loop-based approach to traverse an array in reverse order?
  • A
    Requires more memory
  • B
    Slower execution time
  • C
    Requires more lines of code
  • D
    None of the above
Explanation:
What is the auxiliary space used by the loop-based approach to traverse an array in reverse order?
  • A
    O(1)
  • B
    O(log N)
  • C
    O(N)
  • D
    O(N^2)
Explanation:
Which of the following is a disadvantage of using recursion to traverse an array in reverse order?
  • A
    Requires more memory due to the recursive stack
  • B
    Slower execution time due to function call overhead
  • C
    Requires more lines of code
  • D
    All of the above
Explanation:
What is the time complexity of the recursive approach to traverse an array in reverse order?
  • A
    O(1)
  • B
    O(log N)
  • C
    O(N)
  • D
    O(N^2)
Explanation:
Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Next Article

Similar Reads