GCD of Odd and Even Sums (LeetCode 3658): Solution and Explanation
Given n, add up the first n odd numbers and the first n even numbers, then return the greatest common divisor of those two sums.
Intuition
Draw the odd numbers as L-shaped layers. 1 is a single square, 3 wraps it into a 2x2 square, 5 wraps that into a 3x3 square. So the first n odd numbers always fill an n x n square and add up to n * n.
Do the same with the even numbers. 2 is a 1x2 strip, 4 grows it into a 2x3 rectangle, 6 into 3x4. The first n even numbers fill an n x (n + 1) rectangle and add up to n * (n + 1).
Both sums have n as a factor. What remains is n and n + 1, two consecutive integers, which never share a factor bigger than 1. So the GCD is exactly n.
You can still run Euclid on the two closed forms to see it happen: the rectangle minus the square leaves a single column of n cells, and n divides the square evenly.
Approach: Closed forms plus Euclid
- Compute
sum_odd = n * n. - Compute
sum_even = n * (n + 1). - Set
a = sum_even,b = sum_odd. - While
bis not 0, replace(a, b)with(b, a % b). - Return
a, which always equals n.
Step-by-step example
n = 4
- The odd numbers 1, 3, 5, 7 stack into a 4x4 square, so sum_odd = 16.
- The even numbers 2, 4, 6, 8 stack into a 4x5 rectangle, so sum_even = 20.
- Euclid: 20 = 1 x 16 + 4. The remainder 4 is the extra column of the rectangle. Now a = 16, b = 4.
- 16 = 4 x 4 + 0. The remainder is 0, so the loop ends with a = 4.
- The function returns 4, which is n.
Code: Python, Java, C++
1class Solution:
2 def gcdOfOddEvenSums(self, n: int) -> int:
3 sum_odd = n * n
4 sum_even = n * (n + 1)
5 a, b = sum_even, sum_odd
6 while b:
7 a, b = b, a % b
8 return a1class Solution {
2 public int gcdOfOddEvenSums(int n) {
3 int sumOdd = n * n;
4 int sumEven = n * (n + 1);
5 int a = sumEven, b = sumOdd;
6 while (b != 0) {
7 int r = a % b;
8 a = b;
9 b = r;
10 }
11 return a;
12 }
13}1class Solution {
2public:
3 int gcdOfOddEvenSums(int n) {
4 int sumOdd = n * n;
5 int sumEven = n * (n + 1);
6 int a = sumEven, b = sumOdd;
7 while (b != 0) {
8 int r = a % b;
9 a = b;
10 b = r;
11 }
12 return a;
13 }
14};Time and space complexity
O(1)
The sums come from formulas, and Euclid on n * (n + 1) and n * n always finishes after two rounds.
O(1)
Only a few integers are stored.
Other approaches
Return n directly (time O(1), space O(1))
Since gcd(n * n, n * (n + 1)) = n * gcd(n, n + 1) = n, the whole function can be return n. It is correct, but showing the closed forms makes the reason clear to an interviewer.
Simulate the sums with a loop (time O(n), space O(1))
Add the odd and even numbers one by one, then call a gcd routine. It is easy to write and fast enough for n up to 1000, but it does linear work for something a formula gives instantly.
Common mistakes
- Summing odd numbers from 1 to n instead of the first n odd numbers. For n = 4 the odds are 1, 3, 5, 7, not 1 and 3.
- Starting the even numbers at 0. The first even positive integer is 2.
- Writing Euclid with the arguments in a fixed order and dividing by zero when n would be 0. The constraints keep n at least 1, but
while bhandles it anyway.
Frequently asked questions
Why is the sum of the first n odd numbers equal to n squared?
Each odd number 2k - 1 is the L-shaped layer that turns a (k-1) x (k-1) square into a k x k square. After n layers you have an n x n square.
Why is the answer always n?
The sums are n * n and n * (n + 1). Pull out the common factor n and you are left with gcd(n, n + 1), which is 1 because any number dividing both would also divide their difference, 1.
Do I need a gcd library function?
No. A two-line Euclid loop is enough, and with these inputs it stops after two rounds. Returning n directly is also accepted.
Can the sums overflow a 32-bit integer?
Not within the constraints. With n at most 1000, n * (n + 1) is at most 1,001,000.