Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A simple graph has 6 vertices labelled 1–6. Vertex i is adjacent to j exactly when gcd(i, j) = 1 and i ≠ j. How many edges does this graph have?

  1. A. 12
  2. B. 22
  3. C. 4
  4. D. 11

Correct answer: D. 11

Of the C(6,2) = 15 pairs, the 4 sharing a factor are (2,4), (2,6), (3,6), (4,6), leaving 11 edges. Counting gcd(1, 1) = 1 as a self-loop gives 12, counting ordered pairs gives 22, and counting the non-coprime pairs gives 4.

More Coding Interview Problems questions

Exams that ask this topic