simple cpp stack solution 95%similar to next greater element I
\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        stack<int>s;\n        vector<int>ans;\n        for(int i=nums.siz
```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        stack<int>s;\n        vector<int>ans;\n        for(int i=nums.size()-1;i>=0;i--)\n\t\t    s.push(nums[i]);\n        for(int i=nums.size()-1;i>=0;i--)\n        {\n            if(s.size()==0)\n                ans.push_back(-1);\n            else if(s.size()>0&&s.top()>nums[i])\n                ans.push_back(s.top());\n            else if(s.size()>0&&s.top()<=nums[i])\n            {\n                while(s.size()>0&&s.top()<=nums[i])\n                    s.pop();\n                if(s.size()==0)\n                    ans.push_back(-1);\n                else\n                    ans.push_back(s.top());\n            }\n            s.push(nums[i]);\n        }\n        reverse(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```
['Stack', 'C', 'C++']