Extending Next Greater Element 1 Solution | Java Solution
\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        \n        Stack<Integer> stack = new Stack<>();\n        int[] ans= new int[nums
```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        \n        Stack<Integer> stack = new Stack<>();\n        int[] ans= new int[nums.length];\n        \n\t\t//For Circular array adding elements from n-2 to 0\n        //As for nth element first element is 0th element\n        //So when we check for next greater for nth element\n        //We should start looking from 0 to n-2\n        for(int i=nums.length-2;i>=0;i--)\n        stack.push(nums[i]);\n    \n\t    //Next Greater Element 1 Solution\n        for(int i=nums.length-1;i>=0;i--)\n        {\n            while(!stack.isEmpty()&&nums[i]>=stack.peek())\n                stack.pop();\n            \n            if(stack.isEmpty())\n            ans[i] = -1;\n            else\n            ans[i] = stack.peek();\n            \n            stack.push(nums[i]);\n        }\n        \n        return ans;\n    }\n}\n```
['Stack', 'Java']