Where have I made mistake?



Problem code

My solution

My code cannot pass the test cases even.


@dragonemperor You need to debug the code yourself. Not only will this help you develop as a programmer, but in any scenario (job, research, contests), you will face situations where your code doesn’t work.

In case you need to generate test cases where your code is failing, I can suggest a pattern:

  1. Find the test case where your code is failing.

  2. Run the code with a debugger. Use break-points in your IDE to jump to specific points of code.

  3. Once the issue is found and the error fixed, submit.

  4. If unsuccessful, find a new test case and go back to step 1.

If you can’t think of any more test cases, you can generate random tests and check using multiple solvers, as shown here. Since you are coding with C++, I would suggest using Visual Studio as your IDE.


Thank you bro. Your advice worked. I found some errors and I fixed them. I had to change the entire code. I created a test input file, solved using bruteforce and compared with the output of the original code. They are same. So I concluded the code is correct. But when I am submitting, I am getting SIGSEGV. Can you give some guidelines on how to fix that? I tried putting assert statements to my code and found no error, no array index out of bound etc. What other steps can I follow?


One possibility is that your local processor has enough memory to spare while the SPOJ processor doesn’t. Is there a scenario where your program takes up too much memory.

The other possibility is that an edge case is being missed, but the assert statements should have caught that…