 All Problems
Next Greater Element II
medium
stack
arrays
monotonic stack
amazon
google

Given a circular integer array nums, return the next greater number for every element in nums. The next greater number of a number x is the first greater number to its traversal-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1.

Example 1:

Input: 1 2 1
Output: 2 -1 2

Example 2:

Input: 1 2 3 4 3
Output: 2 3 4 -1 4

Constraints:

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

Input format: One line — space-separated integers.

Output format: Space-separated next greater elements.

Run to check your code against the sample cases, or submit to run every case