All problems
Circular Array Next Greater Element
Problem
For each element of a circular array, find the next strictly greater element.
Search up to n-1 positions ahead, wrapping around the end of the array. Iterate over the array twice (2n positions) with a monotonic stack of indices to resolve every element once.
Return a list where result[i] is the next greater value or -1 when none exists.
Hint
Loop twice over the indices; only push on the first pass to avoid duplicates.
Test cases
case 1
in: [[1,2,1]]
expected: [2,-1,2]
case 2
in: [[5,4,3,2,1]]
expected: [-1,5,5,5,5]
case 3
in: [[3,1,4,2]]
expected: [4,4,-1,3]
case 4
in: [[2,2,3]]
expected: [3,3,-1]
case 5
in: [[]]
expected: []
Starter codePython
def next_greater_circular(nums):
# Your code here
pass5 test cases · run your code against them in the editor