Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

How many non-empty contiguous subarrays of [2, 2, 5, -1, 3, 4, 1] have a sum that is a perfect square (0 counts as a square)?

  1. A. 7
  2. B. 4
  3. C. 28
  4. D. 2

Correct answer: A. 7

Of the 28 subarray sums, the squares are 1 (once), 4 (three times), 9 (twice) and 16 (once), so 7; the one negative sum (−1) must be rejected before the square-root test. 4 counts distinct square values, 28 all subarrays, and 2 only the sums equal to 9.

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

Exams that ask this topic