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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the greatest common divisor (GCD) of a group of numbers involves checking every possible number to see if it divides all the numbers in the group evenly. We start with the smallest possible GCD and work our way up, testing each one. The largest number that divides all of them evenly is the GCD.
Here's how the algorithm would work step-by-step:
def find_greatest_common_divisor_of_array(numbers):
smallest_number = min(numbers)
test_number = 1
greatest_common_divisor = 1
while test_number <= smallest_number:
is_common_divisor = True
# Check if the test number divides all numbers evenly
for number in numbers:
if number % test_number != 0:
is_common_divisor = False
break
#If the number is a common divisor update greatest_common_divisor
if is_common_divisor:
greatest_common_divisor = test_number
test_number += 1
#Return the largest number found that divided all numbers
return greatest_common_divisor
To efficiently find the greatest common divisor of a collection of numbers, we avoid checking every possible divisor. The key is to find the GCD of pairs of numbers, progressively reducing the problem to a simpler form using the Euclidean algorithm.
Here's how the algorithm would work step-by-step:
def find_greatest_common_divisor_of_array(number_collection):
def find_greatest_common_divisor(
first_number, second_number
):
while(second_number):
first_number, second_number = \
second_number, first_number % second_number
return first_number
# Handle empty array
if not number_collection:
return 0
# Initialize greatest common divisor with the first number
greatest_common_divisor = number_collection[0]
# Iterate to progressively find GCD
for index in range(1, len(number_collection)):
# Compute GCD between current GCD and the next number
greatest_common_divisor = find_greatest_common_divisor(
greatest_common_divisor,
number_collection[index]
)
# Return the final GCD
return greatest_common_divisor
Case | How to Handle |
---|---|
Empty or null array | Return 0 or throw an exception, depending on the requirements, since the GCD is undefined for an empty set. |
Array with a single element | Return the single element as the GCD since GCD(x) = x. |
Array containing zero | Ignore zero elements as GCD(a, 0) = a; the GCD of the remaining elements can be computed. |
Array containing negative numbers | Take the absolute value of each number, as GCD(a, b) = GCD(|a|, |b|). |
Array with all identical numbers | The GCD is simply that number. |
Array with very large numbers (potential integer overflow) | Use a data type that supports large integers (e.g., long in Java/C++, bigint in Python) to prevent overflow during GCD computation. |
Array where all numbers are powers of the smallest number | The smallest number will be the GCD. |
Array with extremely large size | Ensure the algorithm used (e.g., Euclidean algorithm) is efficient and avoids unnecessary memory allocation. |