🎇 [MONOTONIC STACK] INTUITION & APPROACH
\uD83D\uDCA5 PLEASE CONSIDER UPVOTING\n\n# Intuition\n\nSince the array is circular, we need to consider elements that are at the beginning of the array as poss
* \uD83D\uDCA5 _**PLEASE CONSIDER UPVOTING**_\n***\n# Intuition\n\nSince the array is circular, we need to consider elements that are at the beginning of the array as possible "next greater elements" for elements at the end. Using a stack helps us efficiently track and find the next greater element as we traverse the array, but we need to handle the circular nature by treating the array as though it\'s repeated twice.\n\n# Approach\n\n1. We traverse the array from right to left twice (to simulate the circular nature of the array). This is achieved by iterating over the range `2 * size - 1` down to `0`.\n2. For each element at index `i % size` (to handle circular indexing), we pop from the stack while the top of the stack is less than or equal to the current element (since it cannot be the next greater element for the current element).\n3. If the stack is not empty, the top of the stack is the next greater element for the current element. Otherwise, we store `-1` as there is no next greater element.\n4. If the current index `i` is less than `size`, we are processing an element in the first pass over the array and update the answer array accordingly.\n5. Push the current element onto the stack for future comparisons.\n6. Finally, return the answer array.\n\n# Complexity\n- **Time complexity**:\n\n  The time complexity is $$O(n)$$, where `n` is the length of the array. We traverse the array twice (due to the circular nature), and each element is pushed and popped from the stack at most once.\n\n- **Space complexity**:\n\n  The space complexity is $$O(n)$$ because we use a stack to store elements, and in the worst case, all elements could be on the stack simultaneously.\n\n# Code\n```java\nclass Solution {\n    public int[] nextGreaterElements(int[] arr) {\n        int size = arr.length;\n        int[] ans = new int[size];\n        Stack<Integer> stack = new Stack<>();\n\n        for(int i = 2 * size - 1; i >= 0; i--) {\n            while(!stack.isEmpty() && arr[i % size] >= stack.peek()) {\n                stack.pop();\n            }\n\n            if(i < size) {\n                if(!stack.isEmpty()) {\n                    ans[i] = stack.peek();\n                } else {\n                    ans[i] = -1;\n                }\n            }\n\n            stack.push(arr[i % size]);\n        }\n\n        return ans;\n    }\n}\n```
['Array', 'Stack', 'Monotonic Stack', 'Java']