C++/Python/Java/JavaScript |🚀✅ Using stack (With small modification in next-greater-element-1)
\n# Intuition:\nThe problem requires finding the next greater element for each element in the array, considering the array as a circular array. So, we need to c
\n# Intuition:\nThe problem requires finding the next greater element for each element in the array, considering the array as a circular array. So, we need to compare the elements in the array in a circular manner, and for each element, we need to find the next greater element. One way to do this is to use a stack.\n\n# Approach:\nWe start by initializing a stack and a vector to store the answer. We loop through the array from the end, twice the size of the array, to simulate a circular array. For each element, we compare it with the top element in the stack. If the top element in the stack is smaller than the current element, we pop it from the stack until the top element is greater than the current element or the stack becomes empty. If the stack is not empty, we store the top element as the next greater element for the current element, otherwise, we store -1. Finally, we push the current element into the stack.\n\n# Complexity:\n- Time complexity: The time complexity of the solution is O(n) because we are traversing the array twice, and for each element, we are performing a constant number of operations, which is popping elements from the stack and comparing them with the current element.\n\n- Space complexity: The space complexity of the solution is O(n) because we are using a stack and a vector, each of size n, to store the intermediate results.\n\n---\n# C++\n```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        int n = nums.size();\n        stack<int> st;\n        vector<int> ans(n);\n        for(int i=n*2-1;i>=0;i--){\n            while(!st.empty() && st.top()<=nums[i%n]){\n                st.pop();\n            }\n            if(i<n){\n                if(!st.empty()){\n                    ans[i]=st.top();\n                }\n                else{\n                    ans[i]=-1;\n                }\n            }\n            st.push(nums[i%n]);\n        }\n        return ans;\n    }\n};\n```\n\n---\n# Java\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        Stack<Integer> st = new Stack<>();\n        int[] ans = new int[n];\n        Arrays.fill(ans, -1);\n        for(int i = n * 2 - 1; i >= 0; i--) {\n            while(!st.isEmpty() && st.peek() <= nums[i % n]) {\n                st.pop();\n            }\n            if(i < n) {\n                ans[i] = st.isEmpty() ? -1 : st.peek();\n            }\n            st.push(nums[i % n]);\n        }\n        return ans;\n    }\n}\n\n```\n---\n# Python\n```\nclass Solution(object):\n    def nextGreaterElements(self, nums):\n        n = len(nums)\n        st = []\n        ans = [-1] * n\n        for i in xrange(n * 2 - 1, -1, -1):\n            while st and st[-1] <= nums[i % n]:\n                st.pop()\n            if i < n:\n                ans[i] = st[-1] if st else -1\n            st.append(nums[i % n])\n        return ans\n\n```\n---\n# JavaScript\n```\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nfunction nextGreaterElements(nums) {\n    const n = nums.length;\n    const st = [];\n    const ans = new Array(n).fill(-1);\n    for(let i = n * 2 - 1; i >= 0; i--) {\n        while(st.length && st[st.length - 1] <= nums[i % n]) {\n            st.pop();\n        }\n        if(i < n) {\n            ans[i] = st.length ? st[st.length - 1] : -1;\n        }\n        st.push(nums[i % n]);\n    }\n    return ans;   \n};\n```
['Monotonic Stack', 'Python', 'C++', 'Java', 'JavaScript']