Data Structures & AlgorithmsVery hard

When counting windows whose sum is a perfect square, why is the square test exact only with integer square roots?

  1. A. Floating-point square roots can round incorrectly for large values, so an integer square-root check avoids errors.
  2. B. Floating-point is always exact for integers.
  3. C. Perfect squares are never integers.
  4. D. Only windows of length four can be squares.

Correct answer: A. Floating-point square roots can round incorrectly for large values, so an integer square-root check avoids errors.

Taking a float square root and comparing can misjudge large perfect squares because of rounding, so an integer routine such as isqrt followed by squaring gives an exact test.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic