➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Find Greatest Common Divisor of Array

image image

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.

To read more on gcd check out here

The way I’ll solve it is using recursion

Here’s my solve script

class Solution:
    def findGCD(self, nums: List[int]) -> int:
        def gcd(a, b):
            if b == 0:
                return a
            else:
                return gcd(b, a % b)
        
        nums.sort()
        a,b = nums[0], nums[len(nums)-1]
        r = gcd(a, b)

        return r

image