Sort a stack
Given a stack, sort the elements inside that stack in ascending order using only push and pop operation. You can use one additional stack only.
For eg.
Input Stack : 5 (top)
4
3
2
1
Output Stack : 1 (top)
2
3
4
5
Input format
First line of input consists of an integer representing the size of the stack, N
Second line of input consistes of N space-separated integers representing the elements of the stack
Output format
The only line of output consists of the elements of the stack in sorted order (space-separated)
Constraints
0 <= N <= 1000
Time limit: 1 sec
Be the first one to answer
Add answer anonymously...
Top ZIGRAM Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app