Sorting + Set || Easy to understand || C++
Complexity
Time complexity:O(nlogn)

Space complexity:O(n)
Code
# Complexity
- Time complexity:$$O(nlogn)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:$$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& nums) {
        set<pair<int,int>> st;
        int n = nums.size();
        vector<int> ans(n);
        vector<pair<int,int>> vp(n);
        for(int i=0; i<n; i++)
            vp[i] = {nums[i], i};
        sort(vp.rbegin(), vp.rend());
        for(int i=0; i<n; i++){
            int j = i;
            while(j < n && vp[j].first == vp[i].first){
                pair<int,int> p = {vp[j].second, vp[j].first};
                auto it = st.lower_bound(p);
                if(it == st.end()) ans[vp[j].second] = -1;
                else{
                    it++;
                    if(it == st.end()) ans[vp[j].second] = -1;
                    else ans[vp[j].second] = it -> second;
                }
                j++;
            }
            j = i;
            while(j < n && vp[j].first == vp[i].first){
                st.insert({vp[j].second, vp[j].first});
                j++;
            }
            i = j - 1;
        }
        return ans;
    }
};
```
['C++']