Data Structures & AlgorithmsVery hard
When counting windows whose sum is a perfect square, why is the square test exact only with integer square roots?
- A. Floating-point square roots can round incorrectly for large values, so an integer square-root check avoids errors.
- B. Floating-point is always exact for integers.
- C. Perfect squares are never integers.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In the 'longest subarray with a single removal' interview problem, why is subtracting the minimum from the total a common mistake?
- Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?
- When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the…
- Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer…
- Closed real intervals [(1, 4), (3, 6), (8, 9), (9, 12), (14, 15)] are merged whenever they overlap (sharing an endpoint counts as…
- Over all pairs of DIFFERENT positions of [2, 5, 3, 8, 4], take the product of each pair and sort all these products. What is the 6-th…