C++ || Stack || Easy to understand
\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> ans(n);\n        stack<
```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> ans(n);\n        stack<int> st;\n        for(int i=n-2;i>=0;i--)\n        {\n            // if current element is greater than stack top then insert it and pop top of stack\n            while(st.size()>0 && st.top() <=nums[i]) \n            {\n               st.pop();\n            }\n            st.push(nums[i]);\n        }\n        for(int i=n-1;i>=0;i--)\n        {\n            while(st.size()>0 && st.top() <=nums[i]) \n            {\n               st.pop();\n            }\n            // if stack is empty then no greater element then add -1 else stack top\n            ans[i] = st.size() == 0?-1 : st.top();\n            st.push(nums[i]);\n        }\n        return ans;\n    }\n};\n```
['Stack', 'C', 'C++']