How are these solutions different?

Hey guys I was solving a problem yesterday and I still can’t understand why taking the input in one solution separately passes but the other one doesn’t.
Here’re the two solutions
it could be java specific or maybe I am missing something here, any help is appreciated.

Are you sure you’re reading the complete input?


Pretty sure, the loop in both solutions go from 0 to n-1

Are you sure you’re reading the complete input? (2)

Pay attention to the break clause in the second code.


When the loop breaks, your code stop reading the input. It doesn’t happen on the first code, because you are reading all the input before finishing the program.

Not 100% sure if this is the reason for the code to fail, but you are not reading the complete input, indeed.

1 Like

In that case, consider this test case.


7 6 5 4 3 2 1 8
14 13 12 11 10 9 8 7 6 5 4 3 2 1 15

Compare the outputs of both programs.

1 Like

Thanks for the spoiler! Finally I get what was wrong.

Thanks a lot for helping out! I totally missed that.