Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: Count the pairs (i, j) with i < j in the array [1, 2, ..., 10] whose sum is divisible by 4.

  1. A. 10
  2. B. 15
  3. C. 20
  4. D. 7

Correct answer: A. 10

Group 1..10 by residue mod 4: r0 = {4, 8}, r1 = {1, 5, 9}, r2 = {2, 6, 10}, r3 = {3, 7}; valid pairs are C(2,2) + C(3,2) + 3·2 = 1 + 3 + 6 = 10. Letting j start at i adds 5 self-pairs (15), counting ordered pairs gives 20, and forgetting the r2 + r2 pairs gives 7.

More Coding Interview Problems questions

Exams that ask this topic