Open In App

Reverse an array using Stack

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
7 Likes
Like
Report

Given an array arr[] of size n, the task is to reverse the array using Stack.

Examples:

Input: arr = [10, 20, 30, 40, 50] 
Output: 50 40 30 20 10 
Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.

Input: arr = [ 1 ]
Output: 1
Explanation: Reversing the array does not changes the array as it only has a single element

Also read: Array Reverse – Complete Tutorial

The idea is to push all elements of the array into the stack. Since a stack follows the Last In, First Out (LIFO) principle, popping elements from the stack and storing them back into the array results in a reversed order. This efficiently reverses the array without using extra loops for swapping.

Follow the steps given below to reverse an array using stack. 

  • Create an empty stack.
  • One by one push all elements of the array to stack.
  • One by one pop all elements from the stack and push them back to the array.
C++
Java Python C# JavaScript

Output
400 300 200 100 

Next Article

Similar Reads