Two methods | C++ | O(nlogn) | O(n)
\nO(nlogn) method\nSort our array and make pair of index in a separate array.\nNow take a set and push the indices in that set from the end.\nNow we know the in
```\nO(nlogn) method\nSort our array and make pair of index in a separate array.\nNow take a set and push the indices in that set from the end.\nNow we know the indices in set are of elements which are >= current element.\nTake up second index after taking lower_bound of current index in set.\nExample -> 3 2 1 4 5\nSort -> 1  2  3  4  5\n        2  1  0  3  4\nPush in set from end, first two elements are pushed as they do not have\ntwo greater element to their right.\nset -> 3  4\nNow next elment is 3,0 we can say there are two indices greater than 0 \nand next greater II will be 5\nset -> 0 3 4\nFor 2,1 we have 3 and 4 to the right of 1\nset -> 0 1 3 4\nFor 1,2 we have 3 and 4 to right of 2\n``` \n**Take care of duplicates by not pushing the indices unless number changes.**\n```\nclass Solution {\npublic:\n\tstatic bool cmp(const pair<int,int>& a, const pair<int,int>& b){\n        if(a.first == b.first) return a.second > b.second;\n        return a.first < b.first;\n    }\n    vector<int> secondGreaterElement(vector<int>& nums) {\n    // One way to do is by using set\n        vector<pair<int,int>> vec;\n        int n = nums.size();\n        for(int i=0; i<n; i++){\n            vec.push_back({nums[i],i});\n        }\n        sort(vec.begin(), vec.end(), cmp);\n        vector<int> ans(n,-1);\n        set<int> s;\n        for(int i=n-1; i>=0; i--){\n            int index = vec[i].second;\n            if(!s.empty()){\n                auto it = s.lower_bound(index);\n                if(it != s.end()) it++;\n                if(it != s.end()) ans[index] = nums[*it];\n            }\n            s.insert(index);\n        }\n        return ans;\n    }\n};\n```\n```\nUsing three monotonic stacks\nUse two stacks 1. to store the indices of elements which have not got first greater\n               2. to store the indices of elements which have got first greater\n```\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n    // Using stacks\n        int n = nums.size();\n        stack<int> s1,s2,tmp;\n        vector<int> ans(n,-1);\n        for(int i=0; i<n; i++){\n            int curr = nums[i];\n\t\t\t// if current is greater than previous number\n\t\t\t// we have got first greater to that prev number\n            while(!s1.empty() && curr > nums[s1.top()]){\n                tmp.push(s1.top());\n                s1.pop();\n            }\n\t\t\t// if current number is greater than prev\n\t\t\t// we have got second greater to that prev number\n            while(!s2.empty() && curr > nums[s2.top()]){\n                ans[s2.top()] = curr;\n                s2.pop();\n            }\n\t\t\t// we wanted the stack monotonic so we used temp \n\t\t\t// now when we push in temp our order is greater at top\n\t\t\t// but we want smaller at top so we push now to s2\n            while(!tmp.empty()){\n                s2.push(tmp.top());\n                tmp.pop();\n            }\n            s1.push(i);\n        }\n        return ans;\n    }\n};\n```
['C', 'Sorting']