Java Solution
 # Intuition  \n\n\n\n\n\n\n\n\n\n\n\nBefore jumping to solution try to solve the question yourself, then only you will understand the need of priority queue.\n
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity -->\n<!-- - Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nBefore jumping to solution try to solve the question yourself, then only you will understand the need of priority queue.\n\n# Code\n```\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        Stack<Integer> st = new Stack<>();\n        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> nums[a] - nums[b]);\n        int n = nums.length;\n        int ans[] = new int[n];\n\n        for (int i = 0; i < n; i++) {\n            while (!pq.isEmpty() && nums[i] > nums[pq.peek()])\n                ans[pq.poll()] = nums[i];\n            while (!st.isEmpty() && nums[i] > nums[st.peek()])\n                pq.offer(st.pop());\n            st.push(i);\n        }\n        while (!st.isEmpty())\n            ans[st.pop()] = -1;\n        while (!pq.isEmpty())\n            ans[pq.poll()] = -1;\n        return ans;\n    }\n}\n```
['Java']