Different result for similar code

Hi, I have spent a lot of time regarding this but could not figure out the problem. I submitted a solution during contest time but it did not got accepted. After the competition I removed some non-used lines and the solution got accepted. The code has same logic but different result. Can anyone suggest what I am missing ?

Question => Contest Page | CodeChef

My Contest Solution ( Wrong Answer )=> Solution: 48119659 | CodeChef

My Practice Solution (Accepted Answer ) => Solution: 48287158 | CodeChef

It is because of this line.

ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

We blindly use it without knowing how it works :man_shrugging:

1 Like

Fast i/o is for cin/cout. You’re using printf :expressionless:

1 Like

Got it. Thanks a lot. Yes, I didn’t knew how this works. I recently made a template and copied this code from somewhere.

Got it. Thanks a lot.


After putting this statement, printf and cout will not have synchronization. cout uses a buffer, when it’s full it will dump the text into stdout. Which means, cout will not flush immediately whereas printf does (if I am not wrong).

Hence the output is unpredictable.

1 Like