Hello everyone in this article I will be talking about c++ basics which are mostly used in competitive programming which most beginners are not aware of, let me clarify before starting that I am not going to cover the whole basics of c++.
In CP while solving problems we estimate or assume a rough range of data types.
like for :
10^9 -> int -> 10^9
10^12->long int->10^12
10^18->long long int->10^18
Now let's understand the overflow by an example:
Did you try it? what did you get as output?
see if you try this on any competitive programming contest then you will be getting overflow or wrong output.
in the above example, the range of a is 10^5( not a problem because the range of int is around ~10^9, right?) and the same for b, but if we multiply a and b and stores in c then the range of a*b will be 10^10 but the data type of c is int and as you know the range of int can not be more than ~10^9, so this will give overflow as output.
so, we will have to take data type of c as long or long long, right?
let's try this:
you will be thinking now I took data type of c as long then why again I am getting wrong output, this is because I have told you earlier that calculation always occurs in higher data types, so a and b both have int as data type so the calculation will also occur in int.
Now what to do then? let's understand it finally:
now you will not be getting any overflow or wrong output, because a and b both have long as data type so the calculation will also occur in long and the data type of c is also long so we can store it as well.
Now you must be thinking, why I am introducing array, everyone know what is an array? In an array, I want to make you understand its local and global declaration.
1. we are declaring an array locally then the maximum size of the array could be of the order of 10^5.
2. And if we are declaring an array globally then the maximum size of the array could be of the order of 10^7.
These are the only thing that I want to make you understand in an array that I think most beginners don't have any idea about so in the future take care of global and local declaration of an array.