Next Greater Element II (Circular)

Medium

Description

Given a circular integer array nums, return the next greater number for every element. The next greater number is the first greater number to its right in a circular manner.

Examples

Input:nums = [1,2,1]
Output:[2,-1,2]
Explanation:

Circular array: next greater for 1 at index 2 is 2 at index 1.

Input:nums = [5,4,3,2,1]
Output:[-1,-1,-1,-1,5]
Explanation:

This is a decreasing array. For elements at indices 0-3, there are no greater elements to their right in the circular array (5 is the largest). For element 1 at index 4, the next greater element is 5 at index 0 due to the circular nature.

Input:nums = [2,10,12,1,11]
Output:[10,12,-1,11,12]
Explanation:

For 2: next greater is 10. For 10: next greater is 12. For 12: no greater element exists (it's the maximum). For 1: next greater is 11. For 11: wrapping around circularly, the next greater is 12 at index 2.

Constraints

  • 1 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i] ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!