Find Greatest Common Divisor of Array

The task is to find the greatest common divisor (GCD) of the smallest and the largest numbers in a given list.

In Python, we can easily find the smallest and largest numbers in a list using the built-in min() and max() functions. After finding these numbers, we can use the math.gcd() function to find their GCD.

Python solution using these built-in functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from typing import List
import math

class Solution:
    def findGCD(self, nums: List[int]) -> int:
        # Find the smallest and largest numbers in the list
        smallest_num = min(nums)
        largest_num = max(nums)

        # Find and return the GCD of the smallest and largest numbers
        return math.gcd(smallest_num, largest_num)

This solution is efficient, with a time complexity of O(n) as it needs to iterate over all elements in the list to find the smallest and largest numbers. The math.gcd() function has a time complexity of O(log min(a, b)), but since the numbers in the input list are not very large (up to 1000), this operation is relatively fast.