Java (NlogN)  || same as Next Greater Element
\nclass Solution {\n    Stack<Integer> st = new Stack<>();\n    public int[] secondGreaterElement(int[] nums) {\n        st = new Stack<>();\n        int arr[] 
```\nclass Solution {\n    Stack<Integer> st = new Stack<>();\n    public int[] secondGreaterElement(int[] nums) {\n        st = new Stack<>();\n        int arr[] = getNG(nums); \n        PriorityQueue<int[]> pq[] = new PriorityQueue[nums.length];\n        \n        for(int i = 0;i<nums.length;i++){\n            pq[i] = new PriorityQueue<>((a,b)->(a[0]-b[0]));\n        }\n        int[] res = new int[arr.length];\n\n        for(int i = 0;i<nums.length;i++){            \n            if(arr[i]==-1){\n                res[i] = -1;\n                continue;  \n            } \n            pq[arr[i]].add(new int[]{nums[i],i});\n        }\n        \n        st.clear();\n        \n        \n        for(int i = arr.length-1;i>=0;i--){\n            \n            if(st.isEmpty()){\n               while(!pq[i].isEmpty()){\n                   res[pq[i].remove()[1]] = -1;\n               } \n            }\n            else{\n                \n                while(!pq[i].isEmpty()){\n                    int[] x = pq[i].remove();\n                    \n                    while(!st.isEmpty() && x[0]>=st.peek()){\n                        st.pop();\n                    }\n                    \n                    if(st.isEmpty()){\n                        res[x[1]] = -1;\n                    }\n                    else{\n                        res[x[1]] = st.peek();\n                    }\n                }\n            }\n            st.push(nums[i]);\n        }\n        \n        return res;\n    }\n    \n    public int[] getNG(int[] arr){\n        st.clear();\n        int[] res = new int[arr.length];\n        \n        for(int i = arr.length-1;i>=0;i--){\n            \n            if(st.isEmpty()){\n                res[i] = -1;\n            }\n            else{\n                while(!st.isEmpty() && arr[st.peek()]<=arr[i]){\n                    st.pop();\n                }\n                \n                if(st.isEmpty()){\n                    res[i] = -1;\n                }\n                else{\n                    res[i] = st.peek();\n                }\n            }\n            st.push(i);\n        }\n        return res;\n    }\n    \n}\n```
['Stack', 'Heap (Priority Queue)', 'Java']