#1979
Easy Algorithms Find greatest common divisor of array
Array Math Number Theory
79.7% acceptance
Feb 25, 2026
1297
54
Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Solution
Rust
Time O(2^n)
Space O(n)
impl Solution {
pub fn find_gcd(nums: Vec<i32>) -> i32 {
let min_val = *nums.iter().min().unwrap();
let max_val = *nums.iter().max().unwrap();
Self::gcd(min_val, max_val)
}
fn gcd(a: i32, b: i32) -> i32 {
if b == 0 { a } else { Self::gcd(b, a % b) }
}
}