Find Greatest Common Divisor of Array — LeetCode 1979 Python Solution
- Problem
- #1979
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- Input
- nums = [2,5,6,9,10]
- Output
- 2
- Explanation
- The smallest number in nums is 2.
Python solution
class Solution:
def findGCD(self, nums: List[int]) -> int:
return gcd(max(nums), min(nums))Complexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array \textit{nums} |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1979. Find Greatest Common Divisor of Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Number Theory.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1979. Find Greatest Common Divisor of Array?
- LeetCode 1979. Find Greatest Common Divisor of Array is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1979. Find Greatest Common Divisor of Array?
- The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.
- What is the space complexity of LeetCode 1979. Find Greatest Common Divisor of Array?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1979. Find Greatest Common Divisor of Array cover?
- LeetCode 1979. Find Greatest Common Divisor of Array is tagged Array, Math and Number Theory on LeetCode.