ProgrammingHardTCS NQT · Accenture

Find the gcd of the array [84, 126, 210, 462] by folding the Euclidean gcd across the elements, starting from 0. The procedure below runs and prints the result. What is printed?

Find the gcd of the array [84, 126, 210, 462] by folding the Euclidean gcd across the elements, starting from 0. The procedure below runs and prints the result. What is printed?
g = 0
for each x in the array
    g = gcd(g, x)
end for
Print g
  1. A. 252
  2. B. 84
  3. C. 42
  4. D. 462

Correct answer: C. 42

The gcd of 84 and 126 is 42, and 42 divides 210 and 462, so the answer is 42. The lcm of the first two (252), the smallest element (84), and the largest element (462) are the mistakes a student makes when mixing up gcd and lcm or ordering.

More Pseudocode and Code Tracing questions

Exams that ask this topic