Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code.

Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code.
from math import gcd
print(max(gcd(a, 60 - a) for a in range(1, 60) if a != 60 - a))
What is printed?
  1. A. 30
  2. B. 15
  3. C. 20
  4. D. 60

Correct answer: C. 20

gcd(a, 60 − a) = gcd(a, 60), so a must be a multiple of the gcd; 30 needs a = b = 30, which is excluded, leaving 20 (a = 20, b = 40). 30 ignores a ≠ b, 15 is not maximal, and 60 is impossible.

More Coding Interview Problems questions

Exams that ask this topic