Data Structures & AlgorithmsHardCodeforces Div2 B · residues

Problem: How many pairs (i < j) of the array 1..14 have a[i] + a[j] divisible by 7? Run the code.

Problem: How many pairs (i < j) of the array 1..14 have a[i] + a[j] divisible by 7? Run the code.
a = list(range(1, 15))
print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if (a[i] + a[j]) % 7 == 0))
What is printed?
  1. A. 26
  2. B. 15
  3. C. 13
  4. D. 12

Correct answer: C. 13

Each residue mod 7 occurs twice in 1..14: (0,0) gives 1 pair and (1,6), (2,5), (3,4) give 2 × 2 = 4 each, total 13. Counting ordered pairs gives 26, allowing i = j adds 7 + 7 and 14 + 14 for 15, and forgetting the (0,0) class gives 12.

More Coding Interview Problems questions

Exams that ask this topic