Find Greatest Common Divisor of Array

🏠 ⬅️ ➡️

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.

Example 1:

Input: nums = [2,5,6,9,10] Output: 2 Explanation: The smallest number in nums is 2. The largest number in nums is 10. The greatest common divisor of 2 and 10 is 2.

Example 2:

Input: nums = [7,5,6,8,3] Output: 1 Explanation: The smallest number in nums is 3. The largest number in nums is 8. The greatest common divisor of 3 and 8 is 1.

Example 3:

Input: nums = [3,3] Output: 3 Explanation: The smallest number in nums is 3. The largest number in nums is 3. The greatest common divisor of 3 and 3 is 3.

Constraints:

  • 2 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program gcd_of_smallest_and_largest
    implicit none

    integer :: i, n, nums(1000), smallest, largest, gcd

    do while (get_line(n) > 0)
        read (*, *) nums(1:n)

        smallest = minval(nums(1:n))
        largest = maxval(nums(1:n))

        gcd = gcd_of_two(smallest, largest)

        write (*, '(I0)') gcd
    end do

contains

    function gcd_of_two(a, b) result(gcd)
        implicit none

        integer, intent(in) :: a, b
        integer :: gcd

        if (b == 0) then
            gcd = a
        else
            gcd = gcd_of_two(b, mod(a, b))
        end if
    end function gcd_of_two

end program gcd_of_smallest_and_largest
Compiled
Executed
Correct
module greatest_common_divisor
    implicit none
    private
    public :: gcd
contains
    integer function gcd(nums) result(gcd_val)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: smallest, largest

        smallest = minval(nums)
        largest = maxval(nums)

        gcd_val = gcd_helper(smallest, largest)
    end function gcd

    integer function gcd_helper(a, b) result(gcd_val)
        implicit none
        integer, intent(in) :: a, b
        integer :: remainder

        if (b == 0) then
            gcd_val = a
        else
            remainder = mod(a, b)
            gcd_val = gcd_helper(b, remainder)
        end if
    end function gcd_helper
end module greatest_common_divisor

program test_greatest_common_divisor
    use greatest_common_divisor
    implicit none
    integer, parameter :: nums(4) = [2, 5, 6, 9, 10]
    integer :: gcd_val

    gcd_val = gcd(nums)
    write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val

    gcd_val = gcd([7, 5, 6, 8, 3])
    write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val

    gcd_val = gcd([3, 3])
    write (*,*) 'The greatest common divisor of the smallest and largest number in nums is:', gcd_val
end program test_greatest_common_divisor
🌐 Data from online sources
def findGCD(nums):
    min_val, max_val = min(nums), max(nums)
    while max_val % min_val != 0:
        min_val, max_val = max_val % min_val, min_val
    return min_val
  1. First, find the minimum and maximum elements in the nums array.
  2. Initialize two variables min_val and max_val with the values of the minimum and maximum elements, respectively.
  3. Calculate the greatest common divisor (GCD) of min_val and max_val using the Euclidean algorithm. Keep performing the modulo operation and swapping them until max_val % min_val is equal to zero.
  4. Return the min_val as the GCD of the two numbers.
🌐 Data from online sources
int findGCD(vector<int>& nums) {
    auto min_max = minmax_element(nums.begin(), nums.end());
    int min_val = *min_max.first;
    int max_val = *min_max.second;
    while(max_val % min_val != 0) {
        int temp = min_val;
        min_val = max_val % min_val;
        max_val = temp;
    }
    return min_val;
}
  1. First, find the minimum and maximum elements in the nums array.
  2. Initialize two variables min_val and max_val with the values of the minimum and maximum elements, respectively.
  3. Calculate the greatest common divisor (GCD) of min_val and max_val using the Euclidean algorithm. Keep performing the modulo operation and swapping them until max_val % min_val is equal to zero.
  4. Return the min_val as the GCD of the two numbers.