589. Next Greater Element

EasyStackMonotonic StackStack

Given an array nums, for each element return the next strictly greater 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 greater element for each position.

Examples

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

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →