Data Structures & AlgorithmsModerate

How many contiguous subarrays of 3 1 4 1 5 9 2 have (sum − length) divisible by 4?

  1. A. 5
  2. B. 19
  3. C. 9
  4. D. 10

Correct answer: C. 9

Subtracting the length from each element turns the test into a zero residue of a shifted sum, which can be counted with prefix residues. Using the plain sum or adding the length changes the condition.

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

Exams that ask this topic