LeetCode 503 Next Greater Element II - Problem Statement

The problem

Given a circular array, find the next greater element for each position. The array wraps around, so after the last element comes the first.

With [1, 2, 1]:

  • Position 0 (1): next greater is 2 at position 1.
  • Position 1 (2): wrap around. 1, 1 are both smaller. No next greater. Answer: -1.
  • Position 2 (1): wrap to position 0 (1, same). Then position 1 (2). Next greater is 2.

Result: [2, -1, 2].

Constraints: 11 \le length 104\le 10^4.