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 :

Line 1 : No. of elements in stack

Line 2 : Stack elements (separated by space)

Sample Input
5
1 2 3 4 5
Sample Output
1 2 3 4 5

Login to submit your solution

Fb dab53a1a64929320dc7425896aa73fd9e5512b6ab52e414ff5f3ebb5cecc73eb

Share Problem