Data Structures & AlgorithmsHard

How many contiguous subarrays of 1 3 5 2 4 6 1 have a sum that is a perfect square (0 is not present here)?

  1. A. 3
  2. B. 5
  3. C. 2
  4. D. 6

Correct answer: B. 5

Test each subarray's sum with an exact integer square-root check; the product and the shifted sum are different quantities that happen to be squares sometimes. Excluding singletons removes real squares such as 1 and 4.

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

Exams that ask this topic