Sorting + Multiset
\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) \n    {\n        int n =  nums.size();\n        vector<vector<int>> vt;\n  
```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) \n    {\n        int n =  nums.size();\n        vector<vector<int>> vt;\n        for(int i = 0;i<n;i++)\n        {\n            vt.push_back({nums[i] , i});\n        }\n        \n        multiset<int> st;\n        vector<int> ans(n , -1);\n        sort(vt.begin() , vt.end());\n        \n        for(int i = n-1;i>=0;)\n        {\n            vector<int> tp;\n            int val = vt[i][0];\n            while(i >= 0 && val == vt[i][0])\n            {\n                auto itr = st.upper_bound(vt[i][1]);\n                if(itr != st.end())\n                {\n                    if(itr != st.end()) itr++;\n                    if(itr != st.end()) ans[vt[i][1]] = nums[*itr];\n                }\n                tp.push_back(vt[i][1]);\n                i-=1;\n            }\n            \n            for(auto &idx :tp)\n                st.insert(idx);\n        }\n        \n        \n        return ans;\n        \n    }\n};\n```
['Sorting']