1979. Find Greatest Common Divisor of Array LeetCode Solution
In this guide, you will get 1979. Find Greatest Common Divisor of Array LeetCode Solution with the best time and space complexity. The solution to Find Greatest Common Divisor of Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Greatest Common Divisor of Array solution in C++
- Find Greatest Common Divisor of Array solution in Java
- Find Greatest Common Divisor of Array solution in Python
- Additional Resources

Problem Statement of Find Greatest Common Divisor of Array
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.
Example 2:
Input: nums = [7,5,6,8,3]
Output: 1
Explanation:
The smallest number in nums is 3.
The largest number in nums is 8.
The greatest common divisor of 3 and 8 is 1.
Example 3:
Input: nums = [3,3]
Output: 3
Explanation:
The smallest number in nums is 3.
The largest number in nums is 3.
The greatest common divisor of 3 and 3 is 3.
Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1979. Find Greatest Common Divisor of Array LeetCode Solution in C++
class Solution {
public:
int findGCD(vector<int>& nums) {
return __gcd(ranges::min(nums), ranges::max(nums));
}
};
/* code provided by PROGIEZ */
1979. Find Greatest Common Divisor of Array LeetCode Solution in Java
class Solution {
public int findGCD(int[] nums) {
final int mn = Arrays.stream(nums).min().getAsInt();
final int mx = Arrays.stream(nums).max().getAsInt();
return gcd(mn, mx);
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// code provided by PROGIEZ
1979. Find Greatest Common Divisor of Array LeetCode Solution in Python
class Solution:
def findGCD(self, nums: list[int]) -> int:
return math.gcd(min(nums), max(nums))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.