Problem title
Difficulty
Avg time to solve

Aggressive Cows
Moderate
30 mins
Average Marks
Easy
5 mins
Ninja’s Training
Moderate
30 mins
Count Ways To Reach The N-th Stairs
Moderate
30 mins
Rearrange String
Easy
15 mins
Reverse Stack Using Recursion
Easy
21 mins
Maximum Sum Rectangle
Easy
15 mins
Best Time to Buy and Sell Stock
Moderate
20 mins
Subset OR
Moderate
20 mins
Merge Two Sorted Arrays
Moderate
15 mins
83

Reverse Stack Using Recursion

Difficulty: EASY
Contributed By
Avg. time to solve
21 min
Success Rate
80%

Problem Statement

Reverse a given stack of integers using recursion.

Note:

You are not allowed to use any extra space other than the internal stack space used due to recursion.
You are not allowed to use the loop constructs of any sort available as handy. For example: for, for-each, while, etc. 
The only inbuilt stack methods allowed are:
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
Input Format :
The first line of input contains an integer value N, denoting the size of the input Stack.

The second line contains N single space-separated integers, denoting the stack elements, where the last (Nth) element is the TOP most element of Stack.    
Constraints :
0 <= N <= 10^3

Where 'N' is the number of elements in the given stack.

Time Limit: 1 sec
Output Format :
N single space-separated integers in a single line, where the first integer denotes the TOP element of the reversed stack.
Sample Input 1 :
3
2 1 3
Sample Output 1 :
2 1 3
Explanation to Sample Input 1 :
First-line contains an integer 3 denotes the size of the input stack i.e N = 3.
Second-line contains 3 single space-separated integers i.e the elements of the stack.

alt text

Printing the reverse stack starting from the top element: 2 1 3.                    
Sample Input 2 :
2
3 2
Sample Output 2 :
3 2    
Reset Code
Full screen
copy-code
Console