File: find-greatest-common-divisor-of-array/solution.py

Date: 2026-06-06

Time: 16:37

Purpose

This file solves LeetCode #1979 — Find Greatest Common Divisor of Array. Its sole responsibility is providing a Solution class with a findGCD method that computes the GCD of the minimum and maximum elements in an integer array. This follows the repo's convention of one problem per directory, each with a solution.py conforming to LeetCode's class-based submission format.

Key Components

Solution.findGCD(nums: List[int]) -> int — The only method. Takes a list of positive integers (guaranteed length >= 2 by the problem constraints) and returns the GCD of min(nums) and max(nums). The implementation is a single expression — no intermediate state, no iteration beyond what min/max do internally.

Patterns

Dependencies

Imports:

Imported by: The test_solution.py in the same directory. The massive "Imported By" list in the prompt is a red herring — those are test files across the entire repo that each import their *own* directory's solution.py, not this one.

Flow

1. min(nums) — O(n) scan to find the smallest element

2. max(nums) — O(n) scan to find the largest element

3. gcd(smallest, largest) — Euclid's algorithm, O(log(min(a,b))) where a,b are the two values

4. Return the integer result

Total: O(n) time, O(1) space.

Invariants

Error Handling

None. The code trusts LeetCode's constraints. Passing an empty list would raise ValueError from min()/max(). Passing non-integers would propagate whatever math.gcd raises. This is appropriate for a contest submission.

Topics to Explore

Beliefs