Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many pairs (i, j) with 1 <= i < j <= 20 satisfy i j ≡ 1 (mod 7)? Run the code.

Problem: How many pairs (i, j) with 1 <= i < j <= 20 satisfy i * j ≡ 1 (mod 7)? Run the code.
print(sum(1 for i in range(1, 21) for j in range(i + 1, 21) if (i * j) % 7 == 1))
What is printed?
  1. A. 24
  2. B. 48
  3. C. 30
  4. D. 2

Correct answer: A. 24

Inverse pairs mod 7 are (1,1), (2,4), (3,5), (6,6); counting i < j across 1..20 gives 24. Ordered pairs give 48, allowing i = j gives 30, and 2 counts only pairs inside 1..7.

More Coding Interview Problems questions

Exams that ask this topic