Java Solution || Using Stack And Max Heap || TC : O(n) || SC : O(n)
\nclass Solution {\n\n    public int[] secondGreaterElement(int[] nums) {\n        int[] ans = new int[nums.length];\n        Arrays.fill(ans, -1);\n\t\tStack<I
```\nclass Solution {\n\n    public int[] secondGreaterElement(int[] nums) {\n        int[] ans = new int[nums.length];\n        Arrays.fill(ans, -1);\n\t\tStack<Integer> stack = new Stack<>();\n\t\tPriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());\n\t\tfor (int i = 0; i < nums.length; i++) {\n\t\t\twhile (!maxHeap.isEmpty()) {\n\t\t\t\tif (nums[i] <= nums[maxHeap.peek()]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t// find second greater\n\t\t\t\tans[maxHeap.peek()] = nums[i];\n\t\t\t\tmaxHeap.poll();\n\t\t\t}\n\t\t\twhile (!stack.empty()) {\n\t\t\t\tif (nums[i] <= nums[stack.peek()]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t// find first greater, add it PriorityQueue\n\t\t\t\tmaxHeap.add(stack.peek());\n\t\t\t\tstack.pop();\n\t\t\t}\n\t\t\tstack.push(i);\n\t\t}\n\t\treturn ans;\n    }\n}\n```
['Stack', 'Heap (Priority Queue)', 'Java']