Data Structures & AlgorithmsHard

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

  1. A. 12
  2. B. 22
  3. C. 2
  4. D. 10

Correct answer: D. 10

Both residues must be zero on the same block: the length mod 3 and the sum mod 3 are tested together. Dropping one test, or testing residue 1 instead of 0, changes the count.

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

Exams that ask this topic