C++ solution | O(N) time complexity | with proper explaination
Time complexity - O(N)\nSpace complexity - O(N)\n\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n    //circular integer a
**Time complexity - O(N)**\n**Space complexity - O(N)**\n```\nclass Solution {\npublic:\n    vector<int> nextGreaterElements(vector<int>& nums) {\n    //circular integer array -> when we consider first element as the next of the last element hence it\'s circular \n        \n        vector<int> ans(nums.size(), -1); // to store the next greater element of every element and initialize it with -1 why ? because if there is no element in stack which is greater to the element we will return -1\n        \n        stack<int> s; //make a stack \n        \n        for(int i = 0; i < 2*nums.size()-1; i++) { //why ? 2*nums.size()-1 beacuse we assuming a joint imaginary array same as nums\n            \n            while(!s.empty() && nums[s.top()] < nums[i%nums.size()]) {\n                //check  if the stack is not empty  and the check the top element of the stack if there is an element present which is smaller than i% nums.size\n                ans[s.top()] = nums[i%nums.size()];\n                s.pop();// pop the elements which are smaller \n            }\n            s.push(i%nums.size()); \n        }\n        return ans;\n    }\n};\n```\n**Please upvote and comment if have doubt**
['C', 'C++']