[LeetCode]#1979. Find Greatest Common Divisor of Array

Fatboy Slim
Nov 19, 2021

--

Environment: Python 3.8

Key technique: math

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.

Analysis:

  1. Find maximum and minimum in nums.
  2. Calculate common divisor between 1 result.

Solution:

class Solution:
def findGCD(self, nums):
return math.gcd(max(nums), min(nums))

Submissions:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet