ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)?

What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)?
Function gcdList(Array arr)
    Integer r = arr[0]
    for (each x in arr, skipping the first)
        r = gcd(r, x)
    end for
    return r
End Function

Print gcdList({24, 36, 48})
  1. A. 6
  2. B. 4
  3. C. 12
  4. D. 24

Correct answer: C. 12

gcd(24,36)=12, then gcd(12,48)=12. Folding the gcd pairwise across the whole list correctly gives the overall GCD, 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic