Test cases fail to find next greater element the order of output is not correct

Input:
4
7 8 1 4

Its Correct output is:
8 -1 4 -1

And Your Code’s output is:
8 4 -1 -1

please check this code

@rohitkandpal683 don’t print output directly while finding greater element cause then order will change according to values stored in stack.
just store next greater element for each index then after completing search print them in the order of their original index.