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.
It is so because the if ‘2’ is used instead of ‘2.0’ and the argument is passed as parameter in Math.ceil() function, the parameter doesn’t get converted into a floating point implicitly.
Let’s consider by taking an example:
If least area ‘area’ is 100 and base ‘base’ is 17, then
1. int i = (int)Math.ceil(2 * area / base);
= (int)Math.ceil(2 * 100 / 17);
= (int)Math.ceil(200 / 17);
= (intMath.ceil(11);
= (int)11.0;
= 11;
In this case, i = 11, which is wrong.
1. int i = (int)Math.ceil(2.0 * area / base);
= (int)Math.ceil(2.0 * 100 / 17);
= (int)Math.ceil(200.0 / 17);
= (int)Math.ceil(11.764705882352942);
= (int)12.0;
= 12;
In this case, i = 12, which is correct.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Height Triangle
You are viewing a single comment's thread. Return to all comments →
It is so because the if ‘2’ is used instead of ‘2.0’ and the argument is passed as parameter in Math.ceil() function, the parameter doesn’t get converted into a floating point implicitly. Let’s consider by taking an example: If least area ‘area’ is 100 and base ‘base’ is 17, then 1. int i = (int)Math.ceil(2 * area / base); = (int)Math.ceil(2 * 100 / 17); = (int)Math.ceil(200 / 17); = (intMath.ceil(11); = (int)11.0; = 11; In this case, i = 11, which is wrong. 1. int i = (int)Math.ceil(2.0 * area / base); = (int)Math.ceil(2.0 * 100 / 17); = (int)Math.ceil(200.0 / 17); = (int)Math.ceil(11.764705882352942); = (int)12.0; = 12; In this case, i = 12, which is correct.