Use long long or int??

I recently submitted problem NUMGAME from easy practice question.

I submitted the answer but by mistake I took n of integer type and not long long which it must be because its range is given to be 1000000000. However, to my surprise my solution got accepted. I think this must have been because of type promotion during type conversion by compiler. However, then I resubmitted my answer using long long and this time, although it got accepted, it took more memory and ran for more time. So, my question is if someone is getting less time and memory using simple integer instead of long long and the type promotion is always helping in such why someone would have the need to use long long? Why don’t we use integer directly everytime even if takes less space and the compiler converts it internally?
My solution using integer:
My solution using long long:

1 Like

Well. The range for integer type is from -2^31 to 2^31-1=2147483647, so when n<=1000000000 integer is sufficient. If the size of n will be to 10^18, than your solution with integer would be bad. Compiler don’t know how big data you will be read, so when you said integer, it uses integer.

And when you don’t need long longs, you want to use integer type, because it takes less memory and operations with integers are quicker.


But isn’t range of int -32768 to 32767??

I assume, that you start programming with pascal or something similar. In C or C++ is integer 32 bit variable, so it can store 2^32 different values. Check this or this link for more clarity.

@j809 >> What you’re quoting is the minimum range that is guaranteed to be covered by int in C, however, it is of course implementation specific. It depends on the platform and the configuration of the compiler. The most accurate answer that you can get, is using the sizeof() function.

As of here, on Codechef, gcc versions for C and C++ are guaranteed to hold 32 bits for an integer data type.

Coming to the question of which data type to choose, it’s basically having an instinct of how far can your calculations go?! Even if the range in problem is well below 10^9, but say sum or product of two integers is required in calculating something, so in that situation an int type is not going to suffice, you will have to think for a larger type.


In this problem, why we are taking n as long long why not int as range of int is -2^31 to 2^31-1=2147483647 and in question n<=1000000000 which comes in range of int, as in previous explanation its given if we have to sum or product with n then there might be some problem but this is not required in question we are only dividing it,so int is sufficient…
please explain why???

It is not necessary to take long long in this question. You can easily get AC by taking int.

1 Like

Sizeof is an operator, not a function.

See… for n<=10^9. Use “int”. As its range is from -2^31 to +2^31. Around ~10^9.
But in case of n~10^18. “int” wont work. So use “long long int”.
And range can further be increased for 0<n<10^19. by using “unsigned long long int”.
Also, as the size will increase, the computation time will also increase.
Hope, it helps.