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:
|
|
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.