JS Solution ✅|| Explained with Intuition & Approach ✅
Intuition\n Describe your first thoughts on how to solve this problem. \n\n- It is a two loops solution.\n\n# Approach\n Describe your approach to solving the p
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- It is a two loops solution.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe approach used in the below code is :- \n\n1. To iterate over each element in the array and then for each element, iterate over all the other elements until it finds the next greater element. \n\n2. To handle the circular nature of the array, the index is taken modulo n (the length of the array) to wrap around to the beginning of the array.\n\n3. If a greater element is found, it is stored in the answer array at the current index. \n\n4. If no greater element is found after iterating over all the other elements, then -1 is stored in the answer array at the current index.\n\n5. Finally, the answer array is returned, which contains the next greater element for each element in the circular array.\n\n\n# Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n1. Here n is the length of the input array.\n\n2. This is because for each element in the array, the algorithm iterates over all the other elements until it finds the next greater element.\n\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n1. Here n is the length of the input array. \n\n2. This is because the algorithm creates an array to store the result, which has the same length as the input array.\n\n\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar nextGreaterElements = function(nums) {\n    \n    let n = nums.length;\n    \n    let ans = new Array(n);\n    \n    for (let i=0; i<n; i++) {\n        \n        let j = i+1;\n        \n        while (j % n !== i) {\n            \n            if (nums[j % n] > nums[i]) {\n                \n                ans[i] = nums[j % n];\n                \n                break;\n            }\n            \n            j++;\n        }\n        \n        if (j % n == i) {\n            \n            ans[i] = -1;\n        }\n    }\n    \n    return ans;\n};\n```\n\n\n![cat img for upvote on LC.jpeg](https://assets.leetcode.com/users/images/76e25f8e-dfee-422c-a969-ebc9bd6d6ae5_1677827905.7533722.jpeg)\n
['Array', 'Stack', 'Monotonic Stack', 'JavaScript']