I can't imagine any other solution being this simple
IntuitionSuppose i the index of the element which is the first elment greater than the element at index j.
I store all such j's in a vector at index i(vector of
# Intuition
Suppose i the index of the element which is the first elment greater than the element at index j. 
I store all such j's in a vector at index i(vector of vector)
And then again check for the next greater element from the end these are O(n) operataions.

# Approach
So First step is to store the elemetns which have their First greater element at i
so fir[i] is a vector containing all such elements(their indices)

we push into the stack current a[i] after processing all fir[i]

now we iterate from the end again similar to how we find the next greater element. However this time we check against the elements fir[i] if the element on top of the stack is lesser we pop it till we find a greater eleent this will be the second greater element for fir[i][0] we do this for all elements in fir[i].
we sort the fir[i] vector to make sure a single iteration is enough.

It can be seen that this works quite intuitively.
One might ask What if we ave poped an elemnen that could have been the second greater element to some fir[j][x] where j<i.
It can be argued that this will never be the case since if there was some element that was popped it was lesser than fir[i][0]
and a[i] is greater than fir[i][0].

so a[i]> fir[j][x].(since fir[i][0]>all popped and  all popped > fir[j][x]). 
forgive me for the inconsistencies thorughout the approach.
# Complexity
- Time complexity:
O(nlogn) 

- Space complexity:
O(n)

# Code
```cpp []
class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& a) {
        int n=a.size();
        vector<vector<int>> fir(n);
        stack<pair<int,int>> st;
        st.push({a[n-1],n-1});
        vector<int> ans(n,-1);
        for(int i=n-2;i>=0;i--){
            while(!st.empty() && st.top().first<=a[i]){
                st.pop();
            }
            if(!st.empty()){
                fir[st.top().second].push_back(i);
            }
            st.push({a[i],i});
        }
        while(!(st.empty())) st.pop();
        st.push({a[n-1],n-1});
        for(int i=n-2;i>=0;i--){
            sort(fir[i].begin(),fir[i].end(),[&a](const int &p,const int &q){
                if(a[p]<a[q]){
                    return true;
                }else{
                    return false;
                }
            });
            
            for(auto it: fir[i]){
                cout<<i<<" ";
                cout<<a[it]<<" "<<endl;
                while(!st.empty() && st.top().first<=a[it]){
                    st.pop();
                }
                if(!st.empty()){
                    ans[it]=a[st.top().second];
                }
            }
            st.push({a[i],i});
        }
        return ans;
    }
};
```
['Array', 'Sorting', 'Monotonic Stack', 'C++']