Approach Using Stack
Intuition\nThe problem is regarding 2nd greater element to the right. So thought of using stack.\n\n# Approach\n\n. Iterate on the array from left & work on ind
# Intuition\nThe problem is regarding 2nd greater element to the right. So thought of using stack.\n\n# Approach\n\n. Iterate on the array from left & work on index.\n. Use three Stack -\n\n(1) Stack1 -> To hold the elements whose 1st greater hasn\'t been found\n(2) Stack2 -> To hold the elements whose 1st greater has been found & looking for 2nd\n(3) Stack3 -> To hold the elements whose 1st greater has been found & then push it to stack2\n\n\n**Important -> 3rd Stack i.e temp stack is necessary.**\n\n# Complexity\n- Time complexity:\n O(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```java []\nclass Solution {\n    public int[] secondGreaterElement(int[] nums) {\n        \n        /* \n        Approach -\n\n        . Iterate on the array from left & work on index.\n        . Use three Stack -\n\n        (1) Stack1 -> To hold the elements whose 1st greater hasn\'t been found\n        (2) Stack2 -> To hold the elements whose 1st greater has been found & looking for 2nd\n        (3) Stack3 -> To hold the elements whose 1st greater has been found & then push it to stack2\n        */\n\n        Stack<Integer> st1 = new Stack<>(); // 1st Greater Not Found\n        Stack<Integer> st2 = new Stack<>(); // 1st Greater Found & Looking for 2nd\n        Stack<Integer> temp = new Stack<>();\n        \n\n        int res[] = new int[nums.length];\n        Arrays.fill(res, -1); // Fill -1 in all index\n\n        for(int i=0; i<nums.length; i++){\n\n            while(!st2.isEmpty() && nums[i] > nums[st2.peek()])\n              res[st2.pop()] = nums[i];\n\n             while(!st1.isEmpty() && nums[i] > nums[st1.peek()]) // 1st Greater Found\n              temp.push(st1.pop());\n\n             while(!temp.isEmpty())\n              st2.push(temp.pop());\n\n           st1.push(i); // Push index\n        }\n\n      return res;\n    }\n}\n```
['Stack', 'Monotonic Stack', 'Java']