Java basic easy to understand O(N) stack solution
\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        int[] result = new int[n];\n        Stack<Integer>
```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        int[] result = new int[n];\n        Stack<Integer> stack = new Stack<>();\n        \n        for(int i=n-1;i>=0;i--)\n            stack.push(nums[i]);\n        for(int i=n-1;i>=0;i--){\n            while(!stack.empty() && stack.peek()<=nums[i])\n                stack.pop();\n\n            if(stack.empty())\n                result[i] = -1;\n            else\n                result[i] = stack.peek();\n            \n            stack.push(nums[i]);\n        }\n        \n        return result;\n        \n    }\n}\n```\nif  u like it please upvote
[]