[C++] Clean and simple solution O(nlogn)
Here, we use 2 priority queues-pq and pq1.\npq contains all the element for which there are no greater element yet.\npq1 contains all the elements for which one
Here, we use 2 priority queues-pq and pq1.\npq contains all the element for which there are no greater element yet.\npq1 contains all the elements for which one greater element has been found.\nWe iterate the nums array and update both pq and pq1 accordingly,code is easy to understand.\nTime complexity - O(nlogn)\nSpace complexity -O(n).\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        int n=nums.size();\n        vector<int> ans(n,0);\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq,pq1;\n        for(int i=0;i<n;i++) ans[i]=-1;\n        for(int i=0;i<n;i++){\n            int x=nums[i];\n            while(!pq1.empty()){\n                auto p=pq1.top();\n                if(p.first>=x) break;\n                pq1.pop();\n                ans[p.second]=x;\n            }\n            while(!pq.empty()){\n                auto p=pq.top();\n                if(p.first>=x) break;\n                pq.pop(); pq1.push(p);\n            }\n            pq.push({x,i});\n        }\n        return ans;\n    }\n};\n```
['C', 'Heap (Priority Queue)']