Table of contents
1.
Introduction
2.
Problem Statement
3.
Approach
4.
Frequently Asked Questions
4.1.
What is the Time and Space complexity of the approach used to reverse an array?
4.2.
Which property of stack helped us to reverse an array?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Reverse an Array using Stack

Author NISHANT RANA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Stacks

Introduction

Arrays and stacks are a few of the easiest and most important data structures out there. These have a wide range of applications along with there being numerous amount of problems that can be made from them. These are also important from the interviewers' perspective and questions are more often than not asked from these concepts.

Problem Statement

We need to reverse an array of ‘N’ elements using a Stack only. Stack is q linear data structure in which we can add or remove the element from the top of the stack only.

Input 1:

 [1, 2, 3, 4, 5]

 

Output 1: 

[5, 4, 3, 2, 1]

Recommended: Do try the Problem yourself before moving on to the solution.

Must Read Stack Operations

Approach

As we all know, that stack follows the property of Last In First Out (L.I.F.O.). We will make use of this property to reverse an array.

We will follow the following steps:-

  1. We will add all the elements of the array one by one to the Stack.
  2. Now while removing the elements from the stack we know the element which was added in the last will get removed first.
  3. Hence, we will just keep removing the elements from the stack and keep adding those elements to our array from the starting.
  4. After removing all the elements from the stack, we can see that our array will get reversed.

Recommended Topic: Recursion

Refer to the below implementation of the above approach.

public static int[] reverse(int arr[]){
    
    int n = arr.length;
    
    // Initialize a stack of capacity n
    Stack stack = new Stack(n);

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

        // Inserting arr[i] values to the stack
        stack.push(arr[i]);
    }

    // Reverse an array elements
    for (int i = 0; i < n; i++) {

        // Updating the arr[i] values
        arr[i] = stack.pop();
    }

    return arr;
}
You can also try this code with Online Java Compiler
Run Code

 

Time Complexity: The time complexity of the above approach is O(N) (where N is the length of the String) because we are iterating the array twice.

 

Space Complexity: The space complexity for the above approach is O(N) because we are maintaining a Stack that will store all the elements of the array.

Must Read  C Program to Reverse a Number

Frequently Asked Questions

What is the Time and Space complexity of the approach used to reverse an array?

Time Complexity: The time complexity of the approach used to reverse an array is O(N) (where N is the length of the String) because we are iterating the array twice.

Space Complexity: The space complexity for the approach used to reverse an array is O(N) because we are maintaining a Stack that will store all the elements of the array.

Which property of stack helped us to reverse an array?

The Last In First Out(L.I.F.O.) property of the stack helped us to reverse an array.

 

Conclusion

In this blog, we have covered the following things:

  1. We first discussed the Stack approach to solve this problem.
  2. Then we discussed the time and space complexity of the approach used.
     

Recommended Reading:

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, DBMS, System Design, Basics of JavaScript, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio. You can also consider our Mern Stack Course to give your career an edge over others.

Cheers!

Live masterclass