JAVA CODE | 6ms | Two iterations | NGETR
\nclass Solution {\n    public int[] nextGreaterElements(int[] arr) {\n        int ngetr[] = new int[arr.length];\n        Stack<Integer> st = new Stack<>();\n 
```\nclass Solution {\n    public int[] nextGreaterElements(int[] arr) {\n        int ngetr[] = new int[arr.length];\n        Stack<Integer> st = new Stack<>();\n        st.push(0);\n        //1st iteration\n        for(int i = 1 ; i < arr.length ; i++){\n            while(st.size() > 0 && arr[st.peek()] < arr[i])\n            {  //this is the logic for NGETR\n                ngetr[st.peek()] = arr[i];\n                st.pop();\n            }\n            st.push(i);\n        }\n        //second iteration as array is cyclic\n        for(int i = 0 ; i < arr.length ; i++){\n            while(arr[st.peek()] < arr[i])\n            {\n                ngetr[st.peek()] = arr[i];\n                st.pop();\n            }\n        }\n        while(st.size() > 0){\n            ngetr[st.peek()] = -1;\n            st.pop();\n        }\n        return ngetr;\n    }\n\t//NGETR => Next Greater Element to Right\n}\n```\n***Plz Upvote If you like the solution***
['Stack', 'Java']