Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
After you see the algo you will think, wow that was quite easy :D
Now do it without loop, in
O(1)
;)No random tests, no one approve it.
That is wrong.
That's because how type promotions work in C.
This will not work:
long toRet = firstNumber + secondNumber
because result of addition of two ints is also an int, so addition overflows, and then such overflowed result is assigned totoRet
. Addition result is not extended tolong
until actual assignment.However, these two will work:
long toRet = (long)firstNumber + secondNumber
, andreturn (long)firstNumber + secondNumber
They work because you explicitly promote one of operands to
long
, so actual addition is performed with longs and does not overflow.Main difference is: in the first case, you work with ints and you add two ints, receiving int in the result. In two remaining cases, you cast one operand to long, the other gets implicitly promoted to long, so you perform addition on two longs.
Description updated
Explain