Next Greater Element |V
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:\ntime O(N) as each no will be process max 2 time so push and pop in stack will not change time complexity of O(N),\n\n# Space complexity:\nSpace -> O(3N) = O(N)\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar secondGreaterElement = function(nums) {\n    let stack1 = [], stack2 = [];\n    let peek = (no) => {\n        if(no == 1)\n            return nums[stack1[stack1.length - 1]];\n        return nums[stack2[stack2.length - 1]];\n    }  \n    let result = new Array(nums.length).fill(-1);\n    for(let i = 0; i < nums.length; i++){\n        while(stack2.length && peek(2) < nums[i]){\n            result[stack2.pop()] = nums[i];\n        }\n        // this is require to store the value in increasing order so that the min value get processs. Otherwise min value will be blocked by a greater value in stack2. \n        let tempStack = [];\n        while(stack1.length && peek(1) < nums[i]){\n            tempStack.push(stack1.pop());\n        }\n        while(tempStack.length) stack2.push(tempStack.pop());\n        stack1.push(i);\n    }\n    return result;\n};\n```
['JavaScript']