The task is to sort a stack so that the top of the stack has the greatest element. You need to complete the function sort() which sorts the elements present in the given stack.
Example 1
Input: Stack: 7 5 9 2 4
Output: 9 7 5 4 2
Explanation: The stack is sorted in descending order where 9 is the topmost element, followed by 7, 5, 4, and 2 at the bottom.
Example 2
Input: Stack: 13 19 5 8 1
Output: 19 13 8 5 1
Explanation: The stack is sorted such that the greatest element, 19, is at the top, followed by 13, 8, 5, and 1 at the bottom.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
5 7 5 9 2 4
9 7 5 4 2
5 13 19 5 8 1
19 13 8 5 1