595. Next Greater Element in Circular Array

EasyStackMonotonic StackStack

Given a circular array nums (the element after the last is the first), return for each element the next strictly greater element when scanning circularly, 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":[1,2,1]}
Output: [2,-1,2]
Explanation: The last 1 wraps around to the 2.
Example 2
Input: {"nums":[5]}
Output: [-1]
Explanation: Only element.
Example 3
Input: {"nums":[3,2,1]}
Output: [-1,3,3]
Explanation: Smaller elements wrap to 3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →