Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

How many index pairs i < j satisfy 3 divides a[i] + a[j] for a = [1, 2, 3, 4, 5, 6, 7]?

  1. A. 8
  2. B. 11
  3. C. 6
  4. D. 7

Correct answer: D. 7

Bucket values by residue mod 3; a pair sums to a multiple of 3 when the residues are (0,0), (1,2) or (2,1). Counting by residues avoids the O(n^2) double loop, though the loop gives the same total.

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

Exams that ask this topic