Java | 3-stack Solution
\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        int[] res = new int[nums.length];\n        Arrays.fill(res, -1);\n        Stack
```\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        int[] res = new int[nums.length];\n        Arrays.fill(res, -1);\n        Stack<Integer> s = new Stack<>();\n        Stack<Integer> q = new Stack<>();\n        for(int i=0; i<nums.length; i++){\n            while(!q.isEmpty() && nums[q.peek()]<nums[i]){\n                res[q.pop()] = nums[i];\n            }\n            Stack<Integer> temp = new Stack<>();\n            while(!s.isEmpty() && nums[s.peek()]<nums[i]){\n                temp.push(s.pop());\n            }\n            s.push(i);\n            while(!temp.isEmpty()){\n                q.push(temp.pop());\n            }\n        }\n        return res;\n    }\n}\n```
['Java']