C++ solution in O(n) Time Complexity
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nFirstly, we initialize 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirstly, we initialize the vector<int> ans by -1, ans then initialize empty stack. Then we will iterate through the array twice and store the next larger element for the first iterator and then as i > n, we will store the index and then as the next larger element will come by comparision then we will store in the ans vector.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        int n = nums.size();\n\n        vector<int> ans(n,-1);\n        stack<int> st;\n\n        for(int i = 0 ; i<2*n;i++){\n            int x = nums[i%n];\n            while(!st.empty() && nums[st.top()] < x){\n                ans[st.top()] = x;\n                st.pop();\n            }\n            if(i<n)\n                st.push(i);\n        }\n\n        return ans;\n    }\n};\n```
['C++']