Beginner Need Help

I have just started solving problems in code chef.But, many of my submissions show wrong answer,run time error and many a times time limit exceeded. I check my program on other soft wares and it works perfectly there.
When i see other submissions many of them use int long long. Is it necessary.And what are the other precaution i should take before submitting. Please help.

1 Like

If you are a beginner, here are some tips you should follow:

  1. Check the constraints properly. If you are given a variable n whose value is upto 10^7 and you have to find the square of the number, then the square will be upto 10^14 which can not be stored in int. U need long long int. Similarly for floating point numbers, when precision upto 10^-6 is required, u need double data type. If you are not yet comfortable with the data types, use long long int and double for all variables.

  2. To avoid compilation error, you need to follow the standards in a programming language. Simple rule is, don’t use conio.h header file, getch(), clrscr() etc.

  3. If you use an integer array of size 10^6, it is okay, but for 10^7 or 10^9 and so, it will take too much memory causing SIGSEGV error. Stick to 10^6 or if u need, upto 4* 10^6. This is safe size of an array. Also take a good look at the constraints. If you need an array of upto 10^9 (say), then be sure your approach is wrong and there are other simpler approaches for the same program. Also, see if a condition in your constraints is leading to division by zero or any other such undefined behaviours.

  4. For TLE, you need better algorithm. When you are struck at a problem with TLE, google for a better algorithm for your same approach.

1 Like

Please be sure that you dont write output inncorrectly (eg “YES” instead of “Yes”), thats annoying to debug.

dragonemperor said a lot of good things in his comment above; however, I have few remarks about it.

Yes, in most cases when you are beginner and you want to use an array of size 1e8 or something like that - it is because your idea is bad; but ML at Codechef is big, so often it is OK to use such arrays (even if you can get rid of them in most cases).

You may calculate how much memory your program needs, if it is few hundreds of megabytes - that is not too much for Codechef (but in 95% of cases you may solve problem with less amount of memory used).

And for TL, either your solution is completely wrong, or it is implemented in a slow way; you may try to run it on some sort of “worst case” and check how long it works. If your program runs 1 minute when TL is 1 second - you’d better look for other algorithm; but if it works 1.5 seconds when TL is 1 second - often you can optimize your code to make it run fast enough. Precompute some stuff, make some constant optimizations, add few breaks and so on.