Taro Logo

GCD of Odd and Even Sums

#158 Most AskedEasy
Topics:
Math

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

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. Is n a positive integer, and is 1 the smallest value we need to handle?
  2. Is sumOdd the sum of the smallest n positive odd numbers, and sumEven the sum of the smallest n positive even numbers?
  3. Should the result be the positive greatest common divisor of those two sums?
  4. Can n be large enough that adding the numbers one by one overflows a 32-bit integer?
  5. Is the expected return value a single integer?

Brute Force Solution

Approach

Add the first n positive odd numbers and the first n positive even numbers one at a time, then take the greatest common divisor of those two totals.

Here's how the algorithm would work step-by-step:

  1. Start both running totals at zero.
  2. For each count from one through n, add the next odd number to the odd total and the next even number to the even total.
  3. The next odd number is one, then three, then five, and so on. The next even number is two, then four, then six, and so on.
  4. After all n numbers are included, compute the greatest common divisor of the two totals.
  5. That divisor is the answer.

Code Implementation

import math

def gcd_of_odd_and_even_sums(n):
    odd_sum = 0
    even_sum = 0
    for index in range(n):
        odd_sum += 2 * index + 1
        even_sum += 2 * (index + 1)
    return math.gcd(odd_sum, even_sum)

Big(O) Analysis

Time Complexity
O(n) – The loop visits each of the n odd numbers and each of the n even numbers once.
Space Complexity
O(1) – Only the two running totals and a few loop variables are stored.

Optimal Solution

Approach

The sum of the first n positive odd numbers is n squared, and the sum of the first n positive even numbers is n times (n + 1). Those two values are always multiples of n and share no larger common factor, so their greatest common divisor is n.

Here's how the algorithm would work step-by-step:

  1. The first n odd numbers are 1, 3, 5, and so on. Their sum is n squared.
  2. The first n even numbers are 2, 4, 6, and so on. Their sum is n times (n + 1).
  3. n and n + 1 are neighbors, so they share no common factor other than 1.
  4. Factor n out of both sums. What remains are n and n + 1, whose greatest common divisor is 1.
  5. The greatest common divisor of the two sums is therefore n.

Code Implementation

def gcd_of_odd_and_even_sums(n):
    return n

Big(O) Analysis

Time Complexity
O(1) – The closed form returns n directly, with no loop over the numbers.
Space Complexity
O(1) – No extra storage is needed beyond the input.

Edge Cases

n is 1
How to Handle:
The odd sum is 1 and the even sum is 2, so the greatest common divisor is 1, which equals n.
n is 4
How to Handle:
The odd sum is 16 and the even sum is 20, so the greatest common divisor is 4, matching the example.
n is at the top of the allowed range
How to Handle:
Building both sums by addition can overflow a 32-bit integer. The closed form returns n directly.
0/245 completed