Data Structures & AlgorithmsHardCodeforces Div2 B · GATE CS

How many index pairs i < j satisfy (a[i] + a[j]) mod 7 = 0 for the array [3, 4, 7, 10, 11, 14, 21, 5, 9]?

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

Correct answer: D. 8

Bucket each value by its residue mod 7; a pair works when the residues are r and (7 - r) mod 7. Residue 0 pairs only with residue 0 (since 0 + 0 = 0 mod 7), and residues r and 7 - r pair with each other; count each class separately to avoid double counting.

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

Exams that ask this topic