Problem · Array
Next Greater Element in a Circular Array
Learn this problemProblem statement
Given a circular integer array, return the next greater value for every position. The next greater value is the first strictly larger value encountered while moving forward, wrapping from the end of the array to the beginning.
If no strictly greater value exists for a position, return -1 there.
Function
nextGreaterElements(nums: int[]) → int[]Examples
Example 1
nums = [1,2,1]return = [2,-1,2]The last position wraps around and finds 2; the value 2 has no greater value.
Example 2
nums = [3,8,4,1,2]return = [8,-1,8,2,3]Values near the end may find their answer after wrapping to the beginning.
Constraints
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9