C++ | 2nd Layer NGE + MinHeap(Priority Queue) for Filtration
\n# Complexity\n- TC -> O(NlogN) :  SC -> O(3N)\n# Code\n\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        vector<
\n# Complexity\n- TC -> O(NlogN) :  SC -> O(3N)\n# Code\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        vector<vector<int>> vec1(nums.size());  \n        stack<int> st;  \n\n        // for the 1st Layer of NGE(Next greater elem) ->\n        // storing the indices of elems having NGE as nums[i] into vec1[i] \n        for(int i=0;i<nums.size();i++){\n            int ele = nums[i];\n            while(!st.empty() && nums[st.top()]<ele){\n                vec1[i].push_back(st.top());\n                st.pop();\n            }\n            st.push(i);\n        }\n\n        vector<vector<int>> vec2(nums.size()); \n\n        //using Min Heap to take the indices of elems which are less than the 2nd NGE(nums[i]) into vec2[i]\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq; // num -> index\n\n        // for the 2nd Layer of NGE(Next greater elem) ->\n        // storing the indices of elems having 2nd NGE as nums[i] into vec2[i]  (sing minheap to take elems < nums[i])\n        for(int i=0;i<nums.size();i++){\n            int ele = nums[i];\n            while(!pq.empty() && ele>pq.top().first){\n                vec2[i].push_back(pq.top().second);\n                pq.pop();\n            }\n            for(auto j:vec1[i]){\n                pq.push({nums[j],j});\n            }\n        }\n\n        vector<int> ans(nums.size(),-1);\n\n        // putting 2nd NGE into its respective 2nd PSE(previous smaller elem) using the vec2\n        for(int i=nums.size()-1;i>=0;i--){\n            int elem = nums[i];\n            for(auto j:vec2[i]){\n                ans[j]=elem;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']