Data Structures & AlgorithmsHardGATE CS · competitive programming

How many pairs i < j in [2, 7, 3, 5, 8, 4] have A[i] + A[j] divisible by 3?

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

Correct answer: A. 6

Taking residues mod 3 gives 2, 1, 0, 2, 2, 1. The only zero-sum combinations are a residue 1 with a residue 2: 2 × 3 = 6 pairs, namely (2,7), (2,4), (7,5), (7,8), (5,4) and (8,4).

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

Exams that ask this topic