Data Structures & AlgorithmsModerate

For the array [3, -1, -5, 4, 2, -6, 7], how many non-empty contiguous subarrays have a sum divisible by 4? Remainders are taken as the mathematical (non-negative) residue, so −3 is treated as ≡ 1 (mod 4).

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

Correct answer: A. 5

Prefix sums with remainders count pairs of equal residues. Forgetting that the empty prefix has remainder 0 misses subarrays that start at index 0, and C-style remainders keep negative keys that never match the non-negative ones.

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

Exams that ask this topic