590. Next Smaller Element

EasyStackMonotonic StackStack

Given an array nums, for each element return the next strictly smaller element to its right, or -1 if none exists. Return the array of answers. The input is JSON {nums}.

Input: JSON {nums}.

Output: Array — the next smaller element for each position.

Examples

Example 1
Input: {"nums":[4,8,5,2,25]}
Output: [2,5,2,-1,-1]
Explanation: Nearest smaller to the right of each.
Example 2
Input: {"nums":[5]}
Output: [-1]
Explanation: Nothing to the right.
Example 3
Input: {"nums":[4,3,2,1]}
Output: [3,2,1,-1]
Explanation: Each is followed by a smaller.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →