We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
To add to alaniane's comment. Float's are not acceptable for representing 32-bit integers since the float type (at least in Java) is, itself, 32 bits. The largest exact integer representations for floating point numbers will depend on the size of the mantissa (and cannot possibly be larger). Since it's necessary for the mantissa to be smaller than 32 bits (for a float), you will not be able to represent all 32-bit int's. This is not a problem for a double (64 bit floating point representation) because the mantissa is much larger than 32 bits.
Assuming Java uses a 24-bit mantissa for a float (from Wikipedia on IEEE 32-bit float), that would mean a largest integer value of around 10^7. Since n can be as large as 10^5 and c[i] as large as 10^4, the maximum value for the summation could be as high as 10^9 (well above what a 24-bit floating point represenation can represent, exactly).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bill Division
You are viewing a single comment's thread. Return to all comments →
To add to alaniane's comment. Float's are not acceptable for representing 32-bit integers since the float type (at least in Java) is, itself, 32 bits. The largest exact integer representations for floating point numbers will depend on the size of the mantissa (and cannot possibly be larger). Since it's necessary for the mantissa to be smaller than 32 bits (for a float), you will not be able to represent all 32-bit int's. This is not a problem for a double (64 bit floating point representation) because the mantissa is much larger than 32 bits.
Assuming Java uses a 24-bit mantissa for a float (from Wikipedia on IEEE 32-bit float), that would mean a largest integer value of around 10^7. Since n can be as large as 10^5 and c[i] as large as 10^4, the maximum value for the summation could be as high as 10^9 (well above what a 24-bit floating point represenation can represent, exactly).