Skip to main content
Back to problems
#3658
Easy Algorithms

Gcd of odd and even sums

Math Number Theory
84.8% acceptance
Feb 25, 2026
93
12
You are given an integer n. Your task is to compute the GCD (greatest common divisor) of two values: sumOdd: the sum of the first n odd numbers. sumEven: the sum of the first n even numbers. Return the GCD of sumOdd and sumEven.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn gcd_of_odd_even_sums(n: i32) -> i32 {
    // sumOdd = n^2, sumEven = n*(n+1), GCD = n
    n
  }
}