From af8efd3f2bdf485ff1595c521e2588d0aa3ac584 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 18 Jul 2026 03:42:05 +0000 Subject: [PATCH] feat: add solution for 1979. Find Greatest Common Divisor of Array --- .../analysis_daily_20260718.md | 45 +++++++++++++ .../problem.md | 65 +++++++++++++++++++ .../solution_daily_20260718.go | 27 ++++++++ .../solution_daily_20260718_test.go | 28 ++++++++ 4 files changed, 165 insertions(+) create mode 100644 problems/1979-find-greatest-common-divisor-of-array/analysis_daily_20260718.md create mode 100644 problems/1979-find-greatest-common-divisor-of-array/problem.md create mode 100644 problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718.go create mode 100644 problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718_test.go diff --git a/problems/1979-find-greatest-common-divisor-of-array/analysis_daily_20260718.md b/problems/1979-find-greatest-common-divisor-of-array/analysis_daily_20260718.md new file mode 100644 index 0000000..90b0232 --- /dev/null +++ b/problems/1979-find-greatest-common-divisor-of-array/analysis_daily_20260718.md @@ -0,0 +1,45 @@ +# 1979. Find Greatest Common Divisor of Array + +[LeetCode Link](https://leetcode.com/problems/find-greatest-common-divisor-of-array/) + +Difficulty: Easy +Topics: Array, Math, Number Theory +Acceptance Rate: 80.8% + +## Hints + +### Hint 1 + +Read the problem carefully: you don't need the GCD of *every* pair of numbers, and you don't need the GCD of the whole array. You only care about two specific values in `nums`. Which two are they, and how expensive is it to find them? + +### Hint 2 + +The two values you need are the smallest and the largest elements. You can find both in a single linear pass over the array — no sorting required. Once you have `min` and `max`, the rest of the problem reduces to a classic, well-known subroutine. + +### Hint 3 + +Computing the greatest common divisor of two integers is done efficiently with the **Euclidean algorithm**: `gcd(a, b) == gcd(b, a % b)`, repeating until the second argument becomes `0`, at which point the first argument is the answer. Combine "one pass to find min and max" with "one Euclidean GCD" and you're done. + +## Approach + +The problem asks only for the GCD of the smallest and largest numbers in the array, so we never need to consider the other elements beyond scanning them to locate those two extremes. + +1. **Find min and max in one pass.** Initialize both `min` and `max` to the first element, then iterate through the array once, updating `min` when a smaller value appears and `max` when a larger value appears. This is O(n) and avoids the O(n log n) cost of sorting. + +2. **Compute the GCD with the Euclidean algorithm.** The Euclidean algorithm relies on the identity `gcd(a, b) = gcd(b, a mod b)`. Each step replaces the pair `(a, b)` with `(b, a mod b)`, shrinking the numbers quickly. When `b` reaches `0`, `a` holds the greatest common divisor. + +For example, with `nums = [2, 5, 6, 9, 10]`: the single pass gives `min = 2` and `max = 10`. Then `gcd(10, 2)`: `10 mod 2 = 0`, so we move to `gcd(2, 0)`, and since the second argument is `0`, the answer is `2`. That matches the expected output. + +Because every `nums[i]` is at least `1`, both `min` and `max` are positive, so the GCD is always well-defined and at least `1`. When all elements are equal (e.g. `[3, 3]`), `min == max` and the GCD is simply that shared value. + +## Complexity Analysis + +Time Complexity: O(n + log(max)) — O(n) to scan the array for min and max, plus O(log(max)) for the Euclidean algorithm, which is dominated by the scan for typical inputs. +Space Complexity: O(1) — only a constant number of scalar variables are used. + +## Edge Cases + +- **All elements equal** (e.g. `[3, 3]`): `min` and `max` are identical, and `gcd(x, x) = x`. The algorithm returns that value correctly. +- **Coprime min and max** (e.g. `[7, 5, 6, 8, 3]` → `min = 3`, `max = 8`): the GCD is `1`, the smallest possible answer. Make sure the loop terminates and doesn't accidentally return `0`. +- **min or max equal to 1**: since `1 <= nums[i]`, a `1` in the array forces `min = 1`, and the GCD becomes `1`. The Euclidean algorithm handles this naturally. +- **Minimum-length array** (`nums.length == 2`): the constraint guarantees at least two elements, so initializing min/max from the first element is always safe — there's no empty-array case to guard against. diff --git a/problems/1979-find-greatest-common-divisor-of-array/problem.md b/problems/1979-find-greatest-common-divisor-of-array/problem.md new file mode 100644 index 0000000..544c6c5 --- /dev/null +++ b/problems/1979-find-greatest-common-divisor-of-array/problem.md @@ -0,0 +1,65 @@ +--- +number: "1979" +frontend_id: "1979" +title: "Find Greatest Common Divisor of Array" +slug: "find-greatest-common-divisor-of-array" +difficulty: "Easy" +topics: + - "Array" + - "Math" + - "Number Theory" +acceptance_rate: 8079.7 +is_premium: false +created_at: "2026-07-18T03:40:40.004382+00:00" +fetched_at: "2026-07-18T03:40:40.004382+00:00" +link: "https://leetcode.com/problems/find-greatest-common-divisor-of-array/" +date: "2026-07-18" +--- + +# 1979. Find Greatest Common Divisor of Array + +Given an integer array `nums`, return****_the**greatest common divisor** of the smallest number and largest number in _`nums`. + +The **greatest common divisor** of two numbers is the largest positive integer that evenly divides both numbers. + + + +**Example 1:** + + + **Input:** nums = [2,5,6,9,10] + **Output:** 2 + **Explanation:** + The smallest number in nums is 2. + The largest number in nums is 10. + The greatest common divisor of 2 and 10 is 2. + + +**Example 2:** + + + **Input:** nums = [7,5,6,8,3] + **Output:** 1 + **Explanation:** + The smallest number in nums is 3. + The largest number in nums is 8. + The greatest common divisor of 3 and 8 is 1. + + +**Example 3:** + + + **Input:** nums = [3,3] + **Output:** 3 + **Explanation:** + The smallest number in nums is 3. + The largest number in nums is 3. + The greatest common divisor of 3 and 3 is 3. + + + + +**Constraints:** + + * `2 <= nums.length <= 1000` + * `1 <= nums[i] <= 1000` diff --git a/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718.go b/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718.go new file mode 100644 index 0000000..c79b5ff --- /dev/null +++ b/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718.go @@ -0,0 +1,27 @@ +package main + +// Approach: Scan the array once to find the smallest and largest values, then +// compute their greatest common divisor using the Euclidean algorithm +// (gcd(a, b) = gcd(b, a % b) until b == 0). This avoids sorting and runs in +// O(n) time with O(1) extra space. + +func findGCD(nums []int) int { + min, max := nums[0], nums[0] + for _, n := range nums { + if n < min { + min = n + } + if n > max { + max = n + } + } + return gcd(max, min) +} + +// gcd returns the greatest common divisor of a and b via the Euclidean algorithm. +func gcd(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} diff --git a/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718_test.go b/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718_test.go new file mode 100644 index 0000000..d463f48 --- /dev/null +++ b/problems/1979-find-greatest-common-divisor-of-array/solution_daily_20260718_test.go @@ -0,0 +1,28 @@ +package main + +import "testing" + +func TestFindGCD(t *testing.T) { + tests := []struct { + name string + nums []int + expected int + }{ + {"example 1: min 2, max 10, gcd 2", []int{2, 5, 6, 9, 10}, 2}, + {"example 2: min 3, max 8, coprime gcd 1", []int{7, 5, 6, 8, 3}, 1}, + {"example 3: all equal, gcd is the value", []int{3, 3}, 3}, + {"edge case: contains 1 forces gcd 1", []int{1, 6, 9, 12}, 1}, + {"edge case: min divides max evenly", []int{4, 8, 16, 12}, 4}, + {"edge case: two-element coprime pair", []int{13, 17}, 1}, + {"edge case: two-element with divisor", []int{15, 5}, 5}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := findGCD(tt.nums) + if result != tt.expected { + t.Errorf("findGCD(%v) = %d, want %d", tt.nums, result, tt.expected) + } + }) + } +}