Next Greater Element IV || Two Stack || Easy-Way || c++ ||
```\nSet s1 will contain the elements whose next greater element is not found yet.\n\nSet s2 will contain the elements whose next greater element has been found
```\nSet s1 will contain the elements whose next greater element is not found yet.\n\nSet s2 will contain the elements whose next greater element has been found but second greater element is not found yet.\n\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        set<pair<int,int> >s1,s2;\n        int n=nums.size();\n        vector<int>ans(n,-1);\n        \n        for(int i=0;i<n;i++)\n        {\n            while(!s2.empty())\n            {\n                auto to=s2.begin();\n                int val=to->first;\n                if(nums[i]>val)\n                {\n                    ans[to->second]=nums[i];\n                    s2.erase(to);\n                }\n                else\n                {\n                    break;\n                }\n            }\n            \n            while(!s1.empty())\n            {\n                auto to=s1.begin();\n                int val=to->first;\n                if(nums[i]>val)\n                {\n                    s2.insert(*to);\n                    s1.erase(*to);\n                }\n                else{\n                    break;\n                }\n            }\n            s1.insert({nums[i],i});\n        }\n        return ans;\n    }\n};
['Stack', 'C', 'Monotonic Stack']