Data Structures & AlgorithmsHard

How many contiguous subarrays of 1 2 3 4 5 6 have a sum of SQUARES divisible by 5?

  1. A. 6
  2. B. 14
  3. C. 8
  4. D. 7

Correct answer: D. 7

Square each value first, then test the residue of the sum of squares; squaring the sum instead gives a different quantity. Residues of squares mod 5 are only 0, 1 and 4, which is why several blocks qualify.

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

Exams that ask this topic