Will there be a wrong answer sowing message if the order are not same as given in the example.
For Example:
(this will be the output if we apply brute force)
2->3
1->3
3->8
8->-1
6->7
7->-1
5->-1
(this will be the output using stack and o(n) solution)
1->3
2->3
3->8
6->7
5->-1
7->-1
8->-1
as shown above the order are different.
2nd Doubt
By seeing at the o(n) solution order is there any way we can find hint that we are going to use stack(not only for this question but for other stack questions as well)