569. Sort a Stack Using Recursion

EasyStackStackRecursionSorting

Given a stack represented as an array (index 0 is the bottom, the last element is the top), return the stack sorted in ascending order so that the largest element ends up on top. The input is JSON {stack}.

Input: JSON {stack}.

Output: Array — the sorted stack (bottom to top, ascending).

Examples

Example 1
Input: {"stack":[3,1,4,1,5]}
Output: [1,1,3,4,5]
Explanation: Sorted ascending, largest on top.
Example 2
Input: {"stack":[]}
Output: []
Explanation: Empty stays empty.
Example 3
Input: {"stack":[2,1]}
Output: [1,2]
Explanation: Two elements ordered.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →