Solution using lazy priority queues
Code\n\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        int n = nums.size();\n        int k = 2;\n\n        vector
# Code\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        int n = nums.size();\n        int k = 2;\n\n        vector<int> ans(n, -1);\n\n        // queue[i] has the indexes of elements which has seen i greater elements to their right so far\n        vector<priority_queue<int>> queues(k); \n\n        for (int i = 0; i < n; i++) { // each new value to the right\n            for (int j = k - 1; j >= 0; j--) {\n                while (!queues[j].empty() && nums[i] > nums[queues[j].top()]) {\n                    int in = queues[j].top();\n                    queues[j].pop();\n\n                    if (j == k - 1) ans[in] = nums[i];\n                    else {\n                        queues[j + 1].push(in);\n                    }\n                }\n            }\n\n            queues[0].push(i);\n        }\n\n        return ans;\n    }\n};\n```
['C++']