Stack + Priority Queue -> 3 stacks
In order to find second greater element we can use 2 stacks.\nIn first stack we can keep elements which doesnt have any greater elements and in second stack we 
In order to find second greater element we can use 2 stacks.\nIn first stack we can keep elements which doesnt have any greater elements and in second stack we can keep elemenets which already found greater elemenet. As soon as we found greater element compared to elements in second stack that is our answer. However there is a problem. Lets assume we have next array: [3,2,1,5,3,2,1,3]. First stack gonna be 3,2,1, then after we encounter element 5 we can move elements into second stack, but they will be in reversed order: 1,2,3. To fix that we can use temporary stack and put elements from first stuck into temp stuck and then from temp to second stack. As a result we would overcome reversing. Another solution is to use priority queue instead of 3 stacks. Its more intuitive but takes more time O(nLog(n)) compared to O(n).\n\nPriority queue solution:\n# Code\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        vector<int> result(nums.size(), -1);\n        stack<int> first;\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> second;\n\n        for (int i = 0; i < nums.size(); i++) {\n            while (!second.empty() && second.top().first < nums[i]) {\n                result[second.top().second] = nums[i];\n                second.pop();\n            }\n\n            while (!first.empty() && nums[first.top()] < nums[i]) {\n                second.push({nums[first.top()], first.top()});\n                first.pop();\n            }\n\n            first.push(i);\n        }\n\n        return result;\n    }\n};\n```\n3 stacks solution\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        vector<int> result(nums.size(), -1);\n        stack<int> first, second, temp;\n\n        for (int i = 0; i < nums.size(); i++) {\n            while (!second.empty() && nums[second.top()] < nums[i]) {\n                result[second.top()] = nums[i];\n                second.pop();\n            }\n\n            while (!first.empty() && nums[first.top()] < nums[i]) {\n                temp.push(first.top());\n                first.pop();\n            }\n\n            while (!temp.empty()) {\n                second.push(temp.top());\n                temp.pop();\n            }\n\n            first.push(i);\n        }\n\n        return result;\n    }\n};\n```
['C++']