Simple Intuitive Solution || Sets|| explained
We will keep two sets to indicate the states of the elements-\n\tSet 1 will contain the elements whose next greater element is not found yet.\n\tSet 2 will cont
We will keep two sets to indicate the states of the elements-\n\t<ol><li>Set 1 will contain the elements whose next greater element is not found yet.</li>\n\t<li>Set 2 will contain the elements whose next greater element has been found but second greater element is not found yet.</li></ol>\n\tNow we iterate from the beginning. For each element \'x\' we do the following-\n\t<ul><li>At first we will check Set 2. Every element \'y\'  that  is less than \'x\' its second greater element will be \'x\' as we already found its first greater element. Then we erase \'y\' from the set.</li>\n\t<li>Then we traverse Set1. Every element \'y\' that is less than \'x\', its first greater element will be \'x\'. So we discovered the first greater element of \'y\'. Hence, we remove \'y\' from Set 1 and insert it in Set2.\n\t</ol>\n\tAny element that was not erased from Set 2 or  Set 1 will have -1 as its second greater element.\n\t\n\tSample Code:\n```\n\tclass Solution {\n\tpublic:\n\t\tvector<int> secondGreaterElement(vector<int>& arr) {\n\t\t\tset<pair<int,int>>Set1,Set2;// Set contains value and index.\n\t\t\tvector<int>ans(arr.size(),-1);\n\t\t\tfor(int i=0;i<arr.size();i++){\n\t\t\t\t//Check Set2\n\t\t\t\twhile(Set2.size()){\n\t\t\t\t\t auto u = *Set2.begin();\n\t\t\t\t\tint val = u.first;\n\t\t\t\t\tif(val<arr[i]){\n\t\t\t\t\t\tSet2.erase(u);\n\t\t\t\t\t\tans[u.second] = arr[i];\n\t\t\t\t\t}else break;\n\t\t\t\t}\n\t\t\t\t//Check Set1\n\t\t\t\twhile(Set1.size()){\n\t\t\t\t\tauto u = *Set1.begin();\n\t\t\t\t\tint val = u.first;\n\t\t\t\t\tif(val<arr[i]){\n\t\t\t\t\t\tSet2.insert(u);\n\t\t\t\t\t\tSet1.erase(u);\n\t\t\t\t\t}else break;\n\t\t\t\t}\n\t\t\t\tSet1.insert({arr[i],i});\n\t\t\t}\n\t\t\treturn ans;\n\t\t}\n\t};\n```\nAny Suggestions/Feedback will be helpful. Do upvote if you find the solution interesting.
['C', 'Ordered Set']