Java Solution with-out traversing array till 2*N times and without using mod operator:)
Intuition\nUse same logic that we have already used in Next Greater Element-I\n\n# Approach\n\nHere is mine Solution with-out traversing array till 2*N times an
# Intuition\nUse same logic that we have already used in Next Greater Element-I\n\n# Approach\n\n**Here is mine Solution with-out traversing array till 2*N times and without using mod operator to mimic array as a circular one!**\n\nJust take N extra space for result array named result[], use stack to store all the elements of given array named arr[] into it(traverse array from end)\nand use same logic that we have already used in Next Greater Element-I\n\nGot all the test cased Successfully Passed!! :) \n# Complexity\n- Time complexity:\nOverall TC would be O(N)\n\n- Space complexity:\nOverall SC would be O(N)\n\n# Code\n```\nclass Solution {\n    public int[] nextGreaterElements(int[] arr) {\n        if(arr.length == 1) {\n\t\t\treturn new int[] {-1};\n\t\t}\n\t\t\n\t\tint result[] = new int[arr.length];\n\t\tint resultPointer=result.length-1;\n\t\tStack<Integer> stack = new Stack<>();\n\t\t\n\t\tfor (int i = arr.length-1; i >=0 ; i--) {\n\t\t\t/**\n\t\t\t * just traverse the array from end and store all the greater element into it,\n\t\t\t * because we don\'t need to store smallest element i.e\n\t\t\t * if array has 5,4,3,2,1 than stack should have only 5 after end of the loop\n\t\t\t * \n\t\t\t * i.e if array has 1,2,3,4,3 than stack should have 4,3,2,1 after end of the loop\n\t\t\t * rather 3,4,3,2,1 because when we add 3 into stack , next time when we got 4 which\n\t\t\t * is greater than 3 we popped-out 3 from stack and store 4 and so on...\n\t\t\t */\n\t\t\tif(!stack.isEmpty() && stack.peek() <= arr[i]) {\n\t\t\t\tstack.pop();\n\t\t\t}\n\t\t\tstack.push(arr[i]);\n\t\t}\n\n        ** // below code snippet of for loop is as same as \n          // the logic used in Next Greater Element-I solution\n        **\t\t\n        for (int i = arr.length-1; i >=0 ; i--) {\n\t\t\t\n\t\t\twhile(!stack.isEmpty() && stack.peek() <= arr[i]) {\n\t\t\t\tstack.pop();\n\t\t\t}\n\t\t\t\n\t\t\tif(stack.isEmpty()) {\n\t\t\t\tresult[resultPointer--] = -1;\n\t\t\t\tstack.push(arr[i]);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tresult[resultPointer--] = stack.peek();\n\t\t\t\tstack.push(arr[i]);\n\t\t\t}\n\t\t}\n\t\t**    //this code snippet specific to this problem only, \n             // pop all the element from stack until stack \n            //  become   empty or \n           //  we found greater element form arr[arr.length-1]\n        **\n\t\twhile(!stack.empty() && stack.peek() <= arr[arr.length-1]) {\n\t\t\tstack.pop();\n\t\t}\n\n\t\t** // when if stack is not empty that means we have found\n          // at-least one greater element from arr[arr.length-1]\n         // , pop it and store into result[]\n        **\n\t\tif(!stack.empty()) {\n\t\t\tresult[result.length-1] = stack.pop();\n\t\t}\n\t\t\n\t\treturn result;\n    }\n}\n```
['Array', 'Monotonic Stack', 'Java']