Easy Stack + Segment Tree Code (n*logn*logn)
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 SGT{
     public:
     vector<int> tree;
     SGT(int n){
        tree.resize(4*n,-1);
     }
     void buildtree(int idx,int start,int end,vector<int> &arr){
        if(start == end){
            tree[idx] = arr[start];
            return;
        }
        int mid = start + (end - start)/2;
        buildtree(2*idx+1,start,mid,arr);
        buildtree(2*idx+2,mid+1,end,arr);
        tree[idx] = max(tree[2*idx+1],tree[2*idx+2]);
     }
     int query(int idx,int start,int end,int left,int right){
        if(end < left || start > right) return -1;
        if(start >= left && end <= right) return tree[idx];
        int mid = start + (end - start)/2;
        return max(query(2*idx+1,start,mid,left,right),query(2*idx+2,mid+1,end,left,right));
     }
};
class Solution {
public:
int n;
void fillNextGreater(vector<int> &nextGreater,vector<int>& nums){
    stack<int> st;
    for(int i=n-1;i>=0;i--){
        while(!st.empty() && nums[st.top()] <= nums[i]) st.pop();
        nextGreater[i] = st.empty() == true ? n : st.top();
        st.push(i);
    }
}
int binarySearch(SGT &st,int &idx,int &target,vector<int>& nums){
     int left = idx+1;
     int right = n-1;
     int res = -1;
     while(left <= right){
        int mid = left + (right - left)/2;
        int val = st.query(0,0,n-1,left,mid);
        if(val > target){
           res = nums[mid];
           right = mid-1;
        }
        else{
           left = mid + 1;
        }
     }
     return res;
}
    vector<int> secondGreaterElement(vector<int>& nums) {
        n = nums.size();
        SGT st(n);
        st.buildtree(0,0,n-1,nums);
        vector<int> nextGreater(n);
        fillNextGreater(nextGreater,nums);
        vector<int> result(n,-1);
        for(int i=0;i<n;i++){
            int nG = nextGreater[i];
            if(nG == n) continue;
            int target = nums[i];
            int res = binarySearch(st,nG,target,nums);
            if(res == -1) continue;
            result[i] = res;
        }
        return result;
    }
};
```
['C++']