Math Fundamentals18 sections · 814 units
Open in Course

GCD of Arrays

Folding from left

To find GCD of an array [a1, a2, ..., an], fold left: result = a1, then result = gcd(result, a2), then gcd(result, a3), and so on.

Example: gcd([12, 18, 24]). Start with 12. gcd(12, 18) = 6. gcd(6, 24) = 6. Answer is 6.

If any element is 0, skip it (gcd(a, 0) = a). If all elements are 0, GCD is undefined or 0 depending on convention.