C++ | Short and easy to understand code | O(N) time and space
Please upvote if you like this approach \ solution \uD83D\uDE0A\n\n\tclass Solution {\n\t\tpublic:\n\t\t\tvector nextGreaterElements(vector& nums) {\n\t\t\t\tin
**Please upvote if you like this approach \\ solution \uD83D\uDE0A**\n\n\tclass Solution {\n\t\tpublic:\n\t\t\tvector<int> nextGreaterElements(vector<int>& nums) {\n\t\t\t\tint n = nums.size(); \n\t\t\t\tvector<int> v(n, -1);\n\t\t\t\tstack<int> st; \n\n\t\t\tfor(int i = 2*n-1; i>=0; i--) {\n\t\t\t\twhile(!st.empty() && st.top() <= nums[i%n]) {\n\t\t\t\t\tst.pop(); \n\t\t\t\t}\n\t\t\t\tv[i%n] = st.empty() ? -1 : st.top();\n\t\t\t\tst.push(nums[i%n]);\n\t\t\t}\n\t\t\treturn v; \n\t\t}\n\t};\n
['Stack', 'C', 'C++']