Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: In the array [2, 4, 6, 8, 10], count the number of subarrays whose sum is divisible by 4.

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

Correct answer: A. 6

The qualifying subarrays are [4], [8], [2,4,6], [6,8,10], [2,4,6,8] and [4,6,8,10], so 6; halving, a sum is ≡ 0 (mod 4) exactly when the halved sum is even. Single elements only give 2, 15 counts all subarrays, and requiring length ≥ 2 gives 4.

More Coding Interview Problems questions

Exams that ask this topic