🔥100%✅Fastest solution | Detailed explanation | Easy understand
Read the below approach to understand the logic\n\nPlease upvote it you like it!!!!!\n\nApproach\n\nThe approach is simple, As mentioned in problem considred gi
**Read the below approach to understand the logic**\n\n***Please upvote it you like it!!!!!***\n\n**Approach**\n\nThe approach is simple, As mentioned in problem considred given array as a cyclic, So firstly take a stack and push all the elments of given array from last to first. Take an another loop iterate from last to first index and inside this loop perform below operation.\n* Take another loop and pop stack elements till stack top element is less than or equal to current index element and stack is not empty.\n* after previous iterations if stack became empty then set -1 as current index element. Else set the stack top value.\n\n**~Time complexity: O(N)**\n\n**~Space complexity: O(N)**\n\n**Code:-**\n\n```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n        int n=nums.size();\n        stack<int> st;\n        st.push(INT_MIN);\n        for(int i=n-1;i>=0;i--){\n            st.push(nums[i]);\n        }\n        for(int i=n-1;i>=0;i--){\n            int curr=nums[i];\n            while(st.top()!=INT_MIN and st.top()<=curr){\n                st.pop();\n            }\n            nums[i]=st.top()==INT_MIN?-1:st.top();\n            st.push(curr);\n        }\n        return nums;\n    }\n};\n```
['Stack', 'C', 'C++']