C++ Solution with O(n) Time complexity
\n# Code\n\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        vector<int> temp;\n        vector<int> temp2(2*nums.siz
\n# Code\n```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        vector<int> temp;\n        vector<int> temp2(2*nums.size());\n        for(int i=0; i<nums.size(); i++){\n            temp.push_back(nums[i]);\n        }\n        for(int i=0; i<nums.size(); i++){\n            temp.push_back(nums[i]);\n        }\n        stack<int> st;\n        for(int i=temp.size()-1; i>=0; i--){\n            int k=temp[i];\n            while(!st.empty() && st.top()<=k){\n                st.pop();\n            }\n            int nge;\n            if(st.empty()){\n                nge=-1;\n            }\n            else{\n                nge=st.top();\n            }\n            temp2[i]=nge;\n            st.push(k);\n        }\n        vector<int> ans;\n        for(int i=0; i<nums.size(); i++){\n            ans.push_back(temp2[i]);\n        }\n        return ans;\n    }\n};\n```
['Stack', 'C++']