Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code.

Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code.
print(sum(1 for i in range(1, 31) for j in range(i + 1, 31) if (i + j) % 6 == 0))
What is printed?
  1. A. 70
  2. B. 140
  3. C. 80
  4. D. 72

Correct answer: A. 70

Residues mod 6 each occur 5 times: (0,0) and (3,3) give C(5,2) = 10 each and (1,5), (2,4) give 25 each, total 70. Ordered pairs give 140, allowing i = j adds 10 for 80, and 'about one sixth of 435 pairs' gives 72.

More Coding Interview Problems questions

Exams that ask this topic