atulcf24's blog

By atulcf24, 5 weeks ago, In English

adding floating point numbers in sqrt make them more accuracte

i submitted a solution containing

 int sq = sqrt(sum);
            if (sq * sq == sum && sq % 2 == 1) cnt++;

although this seems perfectly fine, it doesn't pass the test case listed below

`14'

'1 10 10 100 1 1 10 1 10 2 10 2 10 1`

this must return 3 as the first,fourth and last number fit the condition but returns 2

the issue with it is that sqrt works well with floating point numbers this code fixes the rounding issues

long long sq = (long long)sqrt(sum + 0.5); // Add 0.5 to handle rounding issues
if (sq * sq == sum && sq % 2 == 1) cnt++;

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By atulcf24, history, 5 months ago, In English

Any problem set or blogs for interactive type problems would be appreciated

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it