💥💡 Next Greater Element II - A Brilliant Algorithmic Solution in Java! 💻💪
\uD83D\uDCA1 Intuition\nWe can use a stack to find the next greater element of each element in the given array. We loop through the array twice to compare each 
## \uD83D\uDCA1 Intuition\nWe can use a stack to find the next greater element of each element in the given array. We loop through the array twice to compare each element with every other element. For each element, we push its index onto the stack. If the current element is greater than the top element of the stack, we pop the stack and update the result array with the current element for the popped index. We continue this process until the stack is empty or the top element of the stack is greater than or equal to the current element. Finally, we return the result array.\n\n## \uD83C\uDFAF Approach\n1. Create an array of size n to store the result.\n2. Fill the result array with -1 initially.\n3. Create a stack using ArrayDeque.\n4. Loop through the array twice, because we need to compare each element with every other element.\n5. Get the current element by taking modulus with n.\n6. While the stack is not empty and the top element of the stack is less than the current element, update the result array with the current element for the popped index.\n7. If the current index is less than n, push it onto the stack.\n8. Return the result array.\n\n\n## \u23F0 Time Complexity\nThe time complexity of this algorithm is `O(n)`, where n is the length of the input array.\n\n## \uD83D\uDCBE Space Complexity\nThe space complexity of this algorithm is `O(n)`, because we are using a stack of size n and an array of size `n` to store the result.\n\n## \uD83D\uDCDD Code\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] nums) {\n        int n = nums.length; // get the length of the array nums\n        int[] res = new int[n]; // create an array of size n to store the result\n        Arrays.fill(res, -1); // fill the result array with -1 initially\n        Deque<Integer> stack = new ArrayDeque<>(); // create a stack using ArrayDeque since its efficient\n        \n        // loop through the array twice, because we need to compare each element with every other element\n        for (int i = 0; i < 2 * n; i++) {\n            int num = nums[i % n]; // get the current element by taking modulus with n\n            while (!stack.isEmpty() && nums[stack.peek()] < num) {\n                // while stack is not empty and top element of stack is less than current element\n                res[stack.pop()] = num; // update result array with current element for top element of stack\n            }\n            if (i < n) {\n                stack.push(i); // push the current index onto the stack if i < n\n            }\n        }\n        return res; // return the result array\n    }\n}\n```\n\n#### Please Upvote if it helped !!
['Array', 'Stack', 'Java']