Optimized solution 🔥 Easy JAVA 🔥 Stack implementation 🔥
Intuition<To solve the problem, we need to find the next greater element for each number in the circular array. A monotonic stack is ideal for this task, as it
# Intuition
<To solve the problem, we need to find the next greater element for each number in the circular array. A monotonic stack is ideal for this task, as it allows us to efficiently track the next greater elements while iterating. By traversing the array twice (to simulate its circular nature), we can ensure all elements are covered.

# Approach
1. Simulate Circular Array:
* To handle the circular nature of the array, iterate over it twice using i % len(nums) to access elements.
2. Use a Monotonic Stack:
* Maintain a stack to store elements in decreasing order. This helps efficiently find the next greater element.
* As we traverse, pop elements from the stack that are smaller than or equal to the current element because they can’t be the next greater element.
3. Result Array:
* Initialize a result array res with -1 to represent elements with no next greater value.
* Update res[i] with the top of the stack when the next greater element is found.
4. Push to Stack:
* Add the current element to the stack for future comparisons.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(n)

# Code
```java []
class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int n=nums.length;
        Stack<Integer> stack=new Stack<>();
        int[] arr=new int[n];
        for(int i=0;i<arr.length;i++){
            arr[i]=-1;
        }
    for(int i=0;i<n*2;i++){
        while(!stack.isEmpty() && nums[stack.peek()] < nums[i%n]){
            arr[stack.pop()]=nums[i%n];
        }
        if(i<n){
            stack.push(i);
        }
    }
    return arr;
    }
}
```
```c++ []
class Solution {
public:
    std::vector<int> nextGreaterElements(std::vector<int>& nums) {
        int n = nums.size();
        std::vector<int> result(n, -1); // Initialize result array with -1
        std::stack<int> st;

        // Traverse the array twice to handle circular nature
        for (int i = 0; i < 2 * n; i++) {
            while (!st.empty() && nums[st.top()] < nums[i % n]) {
                result[st.top()] = nums[i % n];
                st.pop();
            }
            if (i < n) {
                st.push(i);
            }
        }
        return result;
    }
};
```
```python []
class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        n = len(nums)
        result = [-1] * n  # Initialize result array with -1
        stack = []  # Stack to store indices
        
        # Traverse the array twice to handle circular nature
        for i in range(2 * n):
            while stack and nums[stack[-1]] < nums[i % n]:
                result[stack.pop()] = nums[i % n]
            if i < n:
                stack.append(i)
        
        return result
```
![Upvote.jpeg](https://assets.leetcode.com/users/images/300e83b4-8851-45b5-b056-8bf9220e9843_1739542459.3587844.jpeg)

['Array', 'Stack', 'Monotonic Stack', 'C++', 'Java', 'Python3']