#1979

Find Greatest Common Divisor of Array

Easy Euclid's GCD

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the GCD of the smallest and largest numbers in the array.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: GCD of the smallest and largest number in the array.

Why this works: Euclid's rule gcd(a, b) = gcd(b, a % b) shrinks the numbers fast.

Example: nums = [2, 5, 6, 9, 10] → min 2, max 10

gcd(10, 2) → gcd(2, 0) → 2

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Computing the GCD of all elements — the problem asks only for min and max.