JAVA | 2 approaches | Brute + Optimal (Stack) ✅
Please Upvote :D\n##### 1. Brute force approach:\n\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        
### **Please Upvote** :D\n##### 1. Brute force approach:\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        int[] ans = new int[n];\n\n        for (int i = 0; i < n; i++) {\n            ans[i] = -1;\n\n            for (int j = 0; j < n; j++) {\n                if (nums[(i + j) % n] > nums[i]) {\n                    ans[i] = nums[(i + j) % n];\n                    break;\n                }\n            }\n        }\n\n        return ans;\n    }\n}\n\n// TC: O(n ^ 2), SC: O(n)\n```\n##### 2. Optimal approach (Stack):\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length;\n        Stack<Integer> stack = new Stack<>();\n\n        int[] ans = new int[n];\n        Arrays.fill(ans, -1);\n\n        for (int i = 0; i < 2 * n; i++) {\n            while (!stack.isEmpty() && nums[stack.peek()] < nums[i % n]) {\n                ans[stack.pop()] = nums[i % n];\n            }\n\n            if (i < n) stack.push(i);\n        }\n\n        return ans;\n    }\n}\n\n// TC: O(n), SC: O(n)\n```
['Stack', 'Java']