Skip to main content

Find Greatest Common Divisor of Array - Solution & Explanation

EasyArrayMathNumber Theory12 min readAsked at: Amazon, Microsoft, Google +2
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: Given an integer array nums, return the greatest common divisor (GCD) of the smallest and largest numbers in the array. The key observation is that the GCD of the entire array reduces to the GCD of min(nums) and max(nums).

The task splits into two parts: first scan the array to find the minimum and maximum values, then compute their GCD using the Euclidean algorithm. This problem combines simple array traversal with a classic number theory technique.

Approach 1: Iterative Euclidean Algorithm (O(n + log M) time, O(1) space)

Start by iterating through the array once to compute minVal and maxVal. This takes O(n) time and constant space. After that, compute the GCD using the iterative Euclidean algorithm: repeatedly replace (a, b) with (b, a % b) until b == 0. The remaining value a is the GCD.

The Euclidean algorithm works because the GCD of two numbers does not change when replacing the larger number with its remainder after division. Each iteration reduces the size of the numbers quickly, giving a time complexity of O(log M), where M is the larger number. This approach is efficient, avoids recursion overhead, and works well in languages where iterative loops are preferred.

Approach 2: Recursive Euclidean Algorithm (O(n + log M) time, O(log M) space)

This approach uses the same mathematical idea but expresses the Euclidean algorithm recursively. After computing minVal and maxVal from the array, define a recursive function gcd(a, b). If b == 0, return a. Otherwise return gcd(b, a % b).

Each recursive call reduces the problem size exactly like the iterative version. The recursion depth is at most O(log M), which means the extra stack space is also logarithmic. This version is often considered more elegant and mirrors the mathematical definition of the Euclidean algorithm closely.

Both approaches rely on fundamental concepts from math and number theory. The only array operation needed is a single pass to identify the smallest and largest elements.

Recommended for interviews: The iterative Euclidean algorithm is typically preferred. Interviewers expect you to recognize that only the minimum and maximum values matter, then apply the Euclidean algorithm to compute the GCD efficiently. Mentioning both iterative and recursive forms demonstrates understanding of the underlying number theory, but the iterative version is slightly more practical in production code.

Approach 1: Approach 1: Iterative Euclidean Algorithm

This approach involves first identifying the smallest and largest numbers in the array, then using the iterative Euclidean algorithm to find their GCD.

The code follows the Euclidean algorithm to compute the GCD of two numbers by iteratively applying the formula a = b and b = a % b until b becomes 0. The minimum and maximum values are derived from the array to apply this algorithm.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log(min(a, b)))
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Approach 2: Recursive Euclidean Algorithm

In this approach, we use the recursive method to implement the Euclidean algorithm for finding the GCD. Identify the minimum and maximum values in the array, then recursively apply the GCD calculation.

This C code takes a recursive approach to calculate the GCD using Euclidean method, recursively solving for gcd(b, a % b) until b is zero.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log(min(a, b)))
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Simulation

We can simulate according to the problem description. First, find the maximum and minimum values in the array nums, then find the greatest common divisor of the maximum and minimum values.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Iterative Euclidean Algorithm

Time Complexity: O(log(min(a, b)))
Space Complexity: O(1)

Approach 2: Recursive Euclidean Algorithm

Time Complexity: O(log(min(a, b)))
Space Complexity: O(1)

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Euclidean AlgorithmO(n + log M)O(1)Best general solution. Efficient and avoids recursion overhead.
Recursive Euclidean AlgorithmO(n + log M)O(log M)Useful when recursion is preferred or when demonstrating the mathematical definition of GCD.

Video Solution

Find Greatest Common Divisor of Array | LeetCode 1979 | English • CodeClips with Abhishek Ranjan • 3,974 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Greatest Common Divisor of Array easy or hard?
LeetCode classifies this problem as Easy with a high acceptance rate around 80%. The difficulty mainly lies in recognizing that the answer only depends on the smallest and largest elements and then applying the Euclidean GCD algorithm correctly.
Find Greatest Common Divisor of Array Python/Java solution
In Python or Java, first compute the minimum and maximum values from the array. Then implement the Euclidean algorithm either iteratively or recursively to compute gcd(minVal, maxVal). Both implementations run in O(n + log M) time and use constant extra space in the iterative version.
How to solve Find Greatest Common Divisor of Array in O(n)?
Traverse the array once to find the smallest and largest elements. After that, apply the Euclidean algorithm to compute gcd(minVal, maxVal). The array traversal costs O(n) and the GCD calculation costs O(log M), giving an overall complexity of O(n + log M).
What is the best approach for Find Greatest Common Divisor of Array?
The optimal approach finds the minimum and maximum elements in the array, then computes their GCD using the Euclidean algorithm. This works because the problem definition only requires the GCD of the smallest and largest numbers. The overall complexity is O(n + log M), where n is the array length and M is the maximum value.
Is Find Greatest Common Divisor of Array asked at Google/Amazon/Meta?
Problems involving the Euclidean algorithm and GCD calculations appear frequently in technical interviews at large companies such as Google, Amazon, and Meta. This specific problem is categorized as easy but tests knowledge of number theory fundamentals and efficient array traversal.
What data structure is used in Find Greatest Common Divisor of Array?
The problem primarily uses an array as the input data structure. The algorithm only requires a linear scan to compute the minimum and maximum values, followed by a mathematical GCD calculation using the Euclidean algorithm.
What is the time complexity of Find Greatest Common Divisor of Array?
The algorithm takes O(n + log M) time. O(n) comes from scanning the array to determine the minimum and maximum values, and O(log M) comes from the Euclidean algorithm used to compute the GCD of those two numbers.

Ready to solve this problem?

Practice Find Greatest Common Divisor of Array with our built-in code editor and test cases.

Practice on FleetCode