Confused about Codility demo test

15,740

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. ;-)

Share:
15,740
JohnB
Author by

JohnB

"The greatest shortcoming of the human race is our inability to understand the exponential function." Dr. Albert Bartlett

Updated on July 27, 2022

Comments

  • JohnB
    JohnB almost 2 years

    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.

    • 2,147,483,647 (largest signed 32-bit int)
    • 4,294,967,294 (number given by Codility.com unit test)

    Can someone please explain how this program can possibly work properly with 4294967294 as one of the data values in an int[]?

    alt text