C++ Simple Solution using Stack and Min-heap
The idea is to maintain a stack to get the first greater element. Once we get the first greater element for a number, we push it to the min-heap waiting to get 
The idea is to maintain a stack to get the first greater element. Once we get the first greater element for a number, we push it to the min-heap waiting to get the second greater element. For each number, we check if it is greater than the elements in the heap; if yes then the current number is the second greater element for that number.\n\n\n```\n vector<int> secondGreaterElement(vector<int>& nums) {\n        \n        int n=nums.size();\n        vector<int> ans(n,-1);\n        \n        stack<pair<int,int>> st;\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n        \n        for(int i=0;i<n;i++){\n            int curr=nums[i];\n            while(pq.size() && pq.top().first<curr){\n                ans[pq.top().second]=curr;\n                pq.pop();\n            }\n            \n            while(st.size() && st.top().first<curr){\n                pq.push(st.top());\n                st.pop();\n            }\n            \n            st.push({curr,i});\n        }\n        \n        return ans;\n    }\n```
['C', 'Monotonic Stack']