believe me no where you will find this much detailed explanation
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

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

# Code
```cpp []
class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& nums) {
        // jin elements ko nge nhi hata rha hai , woh toh milhi ja rhe hai
        // jinko hta rha hai na , unko dekhenge bas 
        // means nge par aakar jisko jisko mai hatunga uso value assign  karta jaunga ! 
        


        // in the first pass 
        // calculate the nge in the first pass ! 
        // store the nge indices instead of values ! 



        // remenber in the next greater element we make a decreasing monotonic stack while
        // traversing the array from backwards
        
         

        int n = nums.size();

        stack<int>st;
        vector<int>nge(n);
        for(int i = nums.size()-1 ; i >= 0 ; i--)
        {
            while(!st.empty() and nums[st.top()]<=nums[i])
            {
                // i have to remove these bars because after this index i these bars have no
                // meaning 
                st.pop();
            }
            if(!st.empty())
            {
                // if the stack is not empty then we have found the nge
                nge[i] = st.top();
            }
            else
            {
                // if the stack is empty then nge is n (hypothetically after the ending of the array)
                nge[i] = n;
            }
            st.push(i);
        }


        // now listen to my story => 
        // nge2 means the just second greater element 
        
        // lets say we have a number x
        // then nge2[x] is nothing but 
        // the first element greater than x occuring in the array after the index of nge[x]

        
        // proof : nge[x] is the first position or the first index of the array which is greater than x
        // for finding the second position we must begin our search at index nge[x] + 1



        // now what should come to your mind ? 
        // a general case answer would be nge[nge[x]]
        // now whats this ? this is the element which remains in the  
        // monotonic stack after you pop out 
        // all the elements smaller than or equal to the current element from 
        // the monotonic stack that you were maintaining while building up nge



        // but wait , are we missing something ? 
        // yess for sure ! 
        // the numbers that you are popping out at nge[x] may also be the answers for elements 
        // whos nge was nge[x]
        // so you should not miss these ! 



        // so we keep a track of elements whoose nge is y and while working at y i have to serve those 
        // elements 
        

        



        vector<priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>> v(1e5+1);




        // i make a list of elements which i have to serve at each nge


         // v[i] contains a priority queue which contains elements which have 
         // their nge == i 
         // the elements are stored in a priority queue format
        for(int i = 0 ; i < nums.size() ; i++)
        {
            v[nge[i]].push({nums[i],i});
        }

        // dhyan se => some of the elements have their nge === 'n'
        // now , we will definitely be not even visiting n as nge 
        // some elements have their nge === n means they dont even 
        // have a single element in the array => so they obviously dont have a second element 
        
        
        // greater than them in the array so 
        // the answer should be initialized at every positon by -1


        // so whatever elements i will not service will have their 
        // nge2 == -1 ! 


        stack<int>st2;
        st = st2;


        vector<int>ans(nums.size(),-1);
        // initializing the answer array by -1




        // i will mimic the process of nge construction so that at each nge element 
        // i can get the elements i am popping out of the stack
        for(int i = nums.size() -1 ; i >= 0 ; i--)
        {
            while(!st.empty() and nums[st.top()]<=nums[i])
            {
                while(!v[i].empty() and v[i].top().first<nums[st.top()])
                {
                    ans[v[i].top().second] = nums[st.top()];
                    v[i].pop();
                }
                st.pop();
            }
            // in the above loop we easily see the elements which we are popping out 
            // so as to check whether they can serve any of out customers 




            if(!st.empty())
            {
                nge[i] = st.top();
            }
            else
            {
                nge[i] = n;
            }


            // if any elements remain then they cannot be served by the elements being popped out
            // so they will be server by nge[nge[x]]

            while(!v[i].empty())
                {
                    int index = v[i].top().second;
                    if(nge[i]!=n)ans[index] = nums[nge[i]];
                    else ans[index] = -1;
                    v[i].pop();
                }
            st.push(i);
        }

        return ans;

    }
};
```
['Array', 'Stack', 'Sorting', 'Heap (Priority Queue)', 'Monotonic Stack', 'C++']