I took the Codility.com demo test.
My program returned the wrong value for one of the unit tests:
TEST extreme_large_numbers Sequence with extremly large numbers testing arithmetic overflow.
TIME 0.056 s.
RESULT WRONG ANSWER got 2, but it is not equilibrium point, sum[0..1]=4294967294, sum[3..3]=-2
Ok, since I couldn't see the problem in my code at first, I tried running it in a Visual Studio project. Then the compiler caught that 4294967294 was too big for an int. It needs to be an uint, or a long. So I changed everything to long and it worked in VS.
However, the demo test does not let you change the function input/return types to long.
Can someone please explain how this program can possibly work properly with 4294967294 as one of the data values in an int[]?

You only need to take the large range into account while computing the result, the final result will not be out of bounds. Still it seems that your solution is not very efficient, you can reach a linear runtime on this one.
Edit: You can look at the revision history to see code that runs fast and scores 100. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With