LeetCode 503 Next Greater Element II - Why Naive Fails

The trap

For each element, scan the array (wrapping around) to find the first greater element.

Each element might scan almost the entire array. With nn elements, that's O(n2)O(n^2) time.

Can you simulate the circular traversal efficiently?