Wrong Answer after running for 1.14secs,what can be possible bug?

I am solving a question of June Challenge and my solution terminates with WA after running for 1.14 seconds.
I am unable to debug it,everything seems okay.
What can be a possible bug?
Whether long limits are overflowing etc??

It is possible that it passed few tests, and failed later (after 1.14 secs) so I guess there is a chance that the error can be either in the data type used, or somewhere in the logic, which the further tests exploit and initial tests do not.

logic is correct i suppose
problem is with data type only but i cant figure it out
using mod everytime so that it fits in long

You can check it with a sample input file taking very large numbers. And by the way you can also prevent overflow when mod calculations are there using typecasting with (long long). And, I hope you are aware of the basic modular rules.


Always double check your “algorithm” and your understanding of the problem statement!! It’s easy to do a “biased” algorithm that “feels” right and that passes the sample test cases given, but, can fail on harder or corner test cases.

Check boudaries like 1, 0, prime numbers, numbers with no common factors or any other special characteristic dictated by the problem statement in itself (obviously, this varies a lot) and of course be sure to test harder test cases and maximum limits to try and “catch” any pesky overflow bug…

If all of the above fails, then you need to do one of the hardest things to do in a contest, which is to “step back” and try a completely new approach.

As Google’s employee Igor Naverniouk said once: “The way people tend to solve problems is usually DFS… They have an initial idea and they attempt to exploit it to its fullest until they can make it work, but, if you start off with the wrong foot, you will never get to the correct answer.”

This means that you need to try a whole new approach which is totally unrelated to your previous (wrong) attempts…

I’m not saying it’s easy, nor am I saying that this is your case… But, be aware for such possibility if all else fails!!

Good luck,


PS: Even more frustrating is to know what technique to use solve a given problem and lacking the mathematical and implementation skill to implement it #BadLuckBruno

1 Like

You mean the ideal approach should be a BFS instead of DFS :smiley: Good one though. +1