math and geometry November 25, 2022

Find greatest common divisor of array

Time O(nlog(n)) Space O(1) Open original problem

We can solve the problem by using Euclidean algorithm. We will find the greatest common divisor of the first two numbers in the array. Then we will find the greatest common divisor of the greatest common divisor and the next number in the array. We will repeat this process until we reach the end of the array.

class Solution:
    def findGCD(self, nums: List[int]) -> int:
        def gcd(a, b):
            if b == 0:
                return a
            return gcd(b, a % b)
        return gcd(min(nums), max(nums))

Time complexity: O(nlog(n)), n is the length of nums
Space complexity: O(1)