Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?

  1. A. 0
  2. B. 10
  3. C. 5
  4. D. 3

Correct answer: C. 5

Group elements by remainder mod 3 and add C(c, 2) for each group. Residue 0 holds {3, 6, 9}, residue 1 holds {1, 4} and residue 2 holds {2, 5}, so the count is 3 + 1 + 1.

More Coding Interview Problems questions

Exams that ask this topic