Beginner's Mistakes in Competitive Programming


Recently I finished the work of an online workshop for competitive programming, there I worked with my team to introduce newbies into competitive programming.

Here is the list of common mistakes that new competitive programmers do.

COUT<<"Enter the value of T"

 No, you do not need to write such statements. Your code will be reviewed by a computer and it will test it on some inputs which will be given in a specified format mentioned in the input format of the question. Your code will be considered to be correct if and only if it gives exactly same output on those cases.

Initializing Variables Outside loop for Testcases

Never initialize variables outside loop of test cases do it inside test case loop. As when you run one test case its value will change and further results will be calculated with the changed value.

Overlook range of data types

For some cases, you can ignore the range of data type but there are many problems which have corner cases designed such that it will cause overflow when small data types are used. 
For reference

Data TypeRange
Int-2147483648 to 2147483647-215+1 to 215+1
Unsigned Int0 to 42949672950 to 216-1
long-2,147,483,648 to 2,147,483,647-231+1 to 231+1
unsigned long0 to 4,294,967,2950 to 232-1
long long-18,446,744,073,709,551,616 to 18,446,744,073,709,551,615-263+1 to 263+1
unsigned long long0 to 18,446,744,073,709,551,6150 to 264-1

Yet another thing that people tends to ignore when deciding data type of varibales is cases in which input is in a small range but output can be large. Like
int a=100000000;
int b=100000000;
int c=a*b;
in this code a and b both are in range but as product of this two is not in range of int hence it may cause overflow

Type Conversion

Type conversion is another thing that you need to take care of.
Remember doing
int x=1,y=2;
double d=x/y;
This will store 0 in d, to store 0.5 in d use
double d=(double)x/y;
This will convert the expression to double and will also store values after the decimal.

New Line

"What the hell!! My code is all correct but it is still showing wrong answer" yelled the inner me but failed to notice the missing endl...

 You need to print a newline character when it is mentioned or use a new line method to print like System.out.Println in Java.

cin.ignore()

Whenever you need to input a string that is preceded by endl in input stream you need to ignore one character using cin.ignore();
int x;
cin>>x;
string s;
cin>>s;//s will have new line character
//Rather use 
cin.ignore();
cin>>s;

Comments