Most rejected submissions fail for a handful of reasons, and almost none of them are the algorithm. Wrong answers usually come from an unread sentence in the statement, a variable that overflowed, or a case the samples never covered. Time limits are often a slow input method rather than a slow idea. Learning to check for these in order turns a vague frustration into a short routine.
Read the output format again after coding
Statement details are cheap to miss: the number of test cases, whether output lines are separated by blank lines, a required trailing newline, an instruction to print the answer modulo something. After you finish typing, go back and read only the output section. Compare your printed format character by character with the sample output, including spaces.
Then check the constraints once more. If the maximum value of a sum can exceed the range of a 32-bit integer, use 64-bit arithmetic throughout and cast intermediate results. Overflow looks exactly like a logic error, which is why it wastes so much time.
Build a small failure routine
When a submission fails, work in a fixed order instead of guessing:
- Rerun the sample input and diff the output.
- Test the smallest possible input and the largest.
- Test a case with duplicates, zeros, or a single element.
- Compare against a brute force on random small cases.
- Read the code aloud once, slowly, looking only at loop bounds and indices.
The brute force step catches most logic errors quickly, because you can generate hundreds of small cases and compare automatically rather than inventing them by hand.
Fix the off-by-one, then the off-by-one again
Boundary mistakes cluster at the ends of loops and arrays. Decide once whether your ranges are half-open and keep that convention everywhere. When a loop uses an inclusive upper bound, write the bound as a named value and check it against the smallest case by hand. A quick mental trace of a two-element input finds more bugs than staring at the code.
Treat slowness as a measurement
If the verdict is a timeout, find out where the time goes before changing the algorithm. Add a timing print around the input reading; on large inputs, reading with slow formatted input can dominate the runtime by itself. Then look at the complexity of your innermost loop and check whether you reallocated or copied a large container inside it.
Frequently asked
Why does my code pass locally and fail on the judge?
Usually an uninitialised variable, reading past the end of an array, or relying on your machine’s stack size. Local runs can appear to work by luck. Rebuild with all warnings enabled and initialise your arrays.
How many more test cases should I write?
Enough to cover the smallest input, the largest, and one hostile pattern such as all equal values. Three deliberate cases beat thirty random ones that all look the same.
Keep a short list of the mistakes you personally make, and check that list before every submission. After a few weeks the list stops growing, which is the real sign that the fixes have become habits.