From 14bb2197ce8fa4d7d4579ee29347f00ea132dc15 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 15 Jul 2026 03:44:56 +0000 Subject: [PATCH] feat: add solution for 3658. GCD of Odd and Even Sums --- .../3658-gcd-of-odd-and-even-sums/analysis.md | 60 +++++++++++++++++ .../3658-gcd-of-odd-and-even-sums/problem.md | 67 +++++++++++++++++++ .../solution_daily_20260715.go | 28 ++++++++ .../solution_daily_20260715_test.go | 27 ++++++++ 4 files changed, 182 insertions(+) create mode 100644 problems/3658-gcd-of-odd-and-even-sums/analysis.md create mode 100644 problems/3658-gcd-of-odd-and-even-sums/problem.md create mode 100644 problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715.go create mode 100644 problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715_test.go diff --git a/problems/3658-gcd-of-odd-and-even-sums/analysis.md b/problems/3658-gcd-of-odd-and-even-sums/analysis.md new file mode 100644 index 0000000..e96f83b --- /dev/null +++ b/problems/3658-gcd-of-odd-and-even-sums/analysis.md @@ -0,0 +1,60 @@ +# 3658. GCD of Odd and Even Sums + +[LeetCode Link](https://leetcode.com/problems/gcd-of-odd-and-even-sums/) + +Difficulty: Easy +Topics: Math, Number Theory +Acceptance Rate: 86.4% + +## Hints + +### Hint 1 + +Before writing any loops, try to find closed-form expressions for the two sums. The sum of the first `n` odd numbers and the sum of the first `n` even numbers are both classic arithmetic series with well-known formulas. Recognizing them turns this into a pure number-theory question. + +### Hint 2 + +Work out each formula. The first `n` odd numbers are `1, 3, 5, ..., (2n-1)` and the first `n` even numbers are `2, 4, 6, ..., 2n`. Once you have `sumOdd` and `sumEven` as simple expressions in `n`, look at how they factor. A shared factor between them will jump out. + +### Hint 3 + +The sum of the first `n` odd numbers is exactly `n²`, and the sum of the first `n` even numbers is `n(n+1)`. Since `n` and `n+1` are consecutive integers, they are coprime — `gcd(n, n+1) = 1`. Therefore `gcd(n², n(n+1)) = n · gcd(n, n+1) = n`. The answer is simply `n`. + +## Approach + +The problem asks for `gcd(sumOdd, sumEven)`, so start by simplifying both sums. + +- **Sum of the first `n` odd numbers:** `1 + 3 + 5 + ... + (2n-1)`. This is a well-known identity that equals `n²`. (Quick check for `n = 4`: `1 + 3 + 5 + 7 = 16 = 4²`.) +- **Sum of the first `n` even numbers:** `2 + 4 + 6 + ... + 2n = 2(1 + 2 + ... + n) = 2 · n(n+1)/2 = n(n+1)`. (Quick check for `n = 4`: `2 + 4 + 6 + 8 = 20 = 4 · 5`.) + +Now we need `gcd(n², n(n+1))`. Factor out the common `n`: + +``` +gcd(n², n(n+1)) = n · gcd(n, n+1) +``` + +Because `n` and `n+1` are consecutive integers, their greatest common divisor is always `1` (any common divisor would have to divide their difference, which is `1`). So: + +``` +gcd(n², n(n+1)) = n · 1 = n +``` + +The result is just `n`. This gives an `O(1)` solution that returns the input directly. + +For learning value, the accompanying solution still computes `sumOdd = n²` and `sumEven = n(n+1)` explicitly and runs the Euclidean algorithm on them. This mirrors the literal problem statement, is easy to trust, and — reassuringly — always produces `n`, confirming the derivation. Either the one-line `return n` or the explicit-GCD version is fully correct; the explicit version is shown to make the reasoning tangible. + +Example walk-through for `n = 5`: +- `sumOdd = 5² = 25` +- `sumEven = 5 · 6 = 30` +- `gcd(25, 30) = 5`, which equals `n`. ✓ + +## Complexity Analysis + +Time Complexity: O(1) — with the `return n` shortcut. The explicit Euclidean-algorithm variant is O(log n), which is still effectively constant here. +Space Complexity: O(1) — only a few integer variables are used. + +## Edge Cases + +- **`n = 1`:** `sumOdd = 1`, `sumEven = 2`, `gcd(1, 2) = 1`, which equals `n`. The smallest input still fits the formula. +- **Upper bound `n = 1000`:** `sumOdd = 1_000_000` and `sumEven = 1_001_000` comfortably fit in a 32-bit `int`, but Go's `int` is 64-bit on common platforms, so there is no overflow risk. Worth confirming that intermediate products don't overflow for larger constraints. +- **Overflow awareness in general:** if the constraints were much larger, `n²` and `n(n+1)` could exceed a fixed-width integer. Here the constraints are tiny, so ordinary `int` arithmetic is safe. diff --git a/problems/3658-gcd-of-odd-and-even-sums/problem.md b/problems/3658-gcd-of-odd-and-even-sums/problem.md new file mode 100644 index 0000000..0079bf6 --- /dev/null +++ b/problems/3658-gcd-of-odd-and-even-sums/problem.md @@ -0,0 +1,67 @@ +--- +number: "3658" +frontend_id: "3658" +title: "GCD of Odd and Even Sums" +slug: "gcd-of-odd-and-even-sums" +difficulty: "Easy" +topics: + - "Math" + - "Number Theory" +acceptance_rate: 8639.4 +is_premium: false +created_at: "2026-07-15T03:43:29.834554+00:00" +fetched_at: "2026-07-15T03:43:29.834554+00:00" +link: "https://leetcode.com/problems/gcd-of-odd-and-even-sums/" +date: "2026-07-15" +--- + +# 3658. GCD of Odd and Even Sums + +You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values: + + * `sumOdd`: the sum of the smallest `n` positive odd numbers. + + * `sumEven`: the sum of the smallest `n` positive even numbers. + + + + +Return the GCD of `sumOdd` and `sumEven`. + + + +**Example 1:** + +**Input:** n = 4 + +**Output:** 4 + +**Explanation:** + + * Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16` + * Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20` + + + +Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`. + +**Example 2:** + +**Input:** n = 5 + +**Output:** 5 + +**Explanation:** + + * Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25` + * Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30` + + + +Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`. + + + +**Constraints:** + + * `1 <= n <= 10​​​​​​​00` diff --git a/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715.go b/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715.go new file mode 100644 index 0000000..dfcae1c --- /dev/null +++ b/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715.go @@ -0,0 +1,28 @@ +package main + +// 3658. GCD of Odd and Even Sums +// +// Approach: derive closed forms for the two sums. +// sumOdd = 1 + 3 + ... + (2n-1) = n^2 +// sumEven = 2 + 4 + ... + 2n = n(n+1) +// Then gcd(n^2, n(n+1)) = n * gcd(n, n+1) = n, since consecutive +// integers n and n+1 are coprime. So the answer is simply n. +// +// For clarity and to mirror the literal problem statement, this +// implementation computes both sums explicitly and applies the +// Euclidean algorithm; the result always equals n. + +func gcdOfOddEvenSums(n int) int { + sumOdd := n * n + sumEven := n * (n + 1) + return gcd(sumOdd, sumEven) +} + +// gcd returns the greatest common divisor of a and b using the +// iterative Euclidean algorithm. +func gcd(a, b int) int { + for b != 0 { + a, b = b, a%b + } + return a +} diff --git a/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715_test.go b/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715_test.go new file mode 100644 index 0000000..d6504a6 --- /dev/null +++ b/problems/3658-gcd-of-odd-and-even-sums/solution_daily_20260715_test.go @@ -0,0 +1,27 @@ +package main + +import "testing" + +func TestGcdOfOddEvenSums(t *testing.T) { + tests := []struct { + name string + n int + expected int + }{ + {"example 1: n=4 -> gcd(16,20)=4", 4, 4}, + {"example 2: n=5 -> gcd(25,30)=5", 5, 5}, + {"edge case: smallest n=1 -> gcd(1,2)=1", 1, 1}, + {"edge case: n=2 -> gcd(4,6)=2", 2, 2}, + {"edge case: upper bound n=1000 -> 1000", 1000, 1000}, + {"additional: n=7 -> gcd(49,56)=7", 7, 7}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := gcdOfOddEvenSums(tt.n) + if result != tt.expected { + t.Errorf("gcdOfOddEvenSums(%d) = %d, want %d", tt.n, result, tt.expected) + } + }) + } +}