LeetCode Entry
3658. GCD of Odd and Even Sums
Gcd of odds and evens
3658. GCD of Odd and Even Sums easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1421
Problem TLDR
Gcd of odds and evens
Intuition
Calculate sums, calculate gcd.
Or.. return n: sum of odds is n^2, sum of evens is n(n+1).
Approach
- remember gcd as
a/b, bab
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun gcdOfOddEvenSums(n: Int) = run{
fun gcd(a: Int, b: Int): Int = if (b==0)a else gcd(b,a%b)
gcd((1..2*n step 2).sum(), (2..2*n step 2).sum())
}
pub fn gcd_of_odd_even_sums(n: i32) -> i32 { n }
Comments