Monotonic stack soln
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 
# 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)$$ -->\n\n# Code\n```\nclass Solution {\n    /**\n    https://leetcode.com/problems/next-greater-element-iv/solutions/2756668/java-c-python-one-pass-stack-solution-o-n/\n\n    Next greater can be found using monotonic stack. But question asks for \n    next to next greater of current element\n    for [11,13,15,12, 0, 15,12,11,9]\n    ans [15,15,-1,-1,12,-1,-1,-1,-1]\n\n    NNGE of 11 is 15\n    NNGE of 0 is 12.\n\n    Trick here is to use monotonic stack to find NG element of current index.\n    We transfer that result into another stack that compute NG of that newly added\n    index thereby computing NNG of current index.\n\n    Only if we know NGE of a number, we can find NNGE of that very number.\n    If there is no NGE of number, we cant find NNGE of that number.\n    For eg, NGE of 11 is 13 and NNGE of 11 is 15.\n    But NGE of 12 is -1 so NNGE of it is also -1.\n\n    */\n    public int[] secondGreaterElement(int[] nums) {\n        Stack<Integer> nextGreaterStack = new Stack<>();\n        Stack<Integer> nextSecondGreaterStack = new Stack<>();\n        Stack<Integer> trasferFromNextGreaterToNextSecondGreaterStack = new Stack<>();\n\n        int n = nums.length;\n        int[] ans = new int[n];\n        Arrays.fill(ans, -1);\n\n        for (int i = 0; i < n; i++) {\n            // nextSecondGreaterStack computes NNGE of indices in its stack\n            while (!nextSecondGreaterStack.isEmpty()\n                 && nums[nextSecondGreaterStack.peek()] < nums[i]) {\n                ans[nextSecondGreaterStack.pop()] = nums[i];\n            }\n\n            // nextGreaterStack computes NGE of indices in its stack\n            while (!nextGreaterStack.isEmpty() &&\n                    nums[nextGreaterStack.peek()] < nums[i]) {\n                trasferFromNextGreaterToNextSecondGreaterStack.push(\n                        nextGreaterStack.pop()\n                );\n            }\n            nextGreaterStack.push(i);\n\n            // trasferFromNextGreaterToNextSecondGreaterStack transfers \n            // indices from nextGreaterStack to nextSecondGreaterStack\n            // for which NGE has been found out. Only if we know NGE of a number\n            // we can find NNGE of that very number.\n            // we use another stack to transfer to preserve the ordering through \n            // push and pop ops\n            while (!trasferFromNextGreaterToNextSecondGreaterStack.isEmpty()) {\n                nextSecondGreaterStack.push(\n                    trasferFromNextGreaterToNextSecondGreaterStack.pop()\n                );\n            }\n\n        }\n\n        return ans;\n    }\n}\n```
['Java']