/**
* Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int findGCD(int[] nums) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int num : nums) {
min = Math.min(min, num);
max = Math.max(max, num);
}
return gcd(min, max);
}
private int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
}
"""
Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
import math
from typing import List
class Solution:
def findGCD(self, nums: List[int]) -> int:
min_val = min(nums)
max_val = max(nums)
return math.gcd(min_val, max_val)
/**
* Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
/**
* Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int findGCD(vector<int>& nums) {
auto [min_it, max_it] = minmax_element(nums.begin(), nums.end());
return std::gcd(*min_it, *max_it);
}
};
/**
* Problem: LeetCode 1979 - Find Greatest Common Divisor of Array
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var findGCD = function(nums) {
let minVal = Infinity;
let maxVal = -Infinity;
for (let i = 0; i < nums.length; i++) {
if (nums[i] < minVal) minVal = nums[i];
if (nums[i] > maxVal) maxVal = nums[i];
}
const gcd = (a, b) => {
while (b !== 0) {
const temp = a % b;
a = b;
b = temp;
}
return a;
};
return gcd(minVal, maxVal);
};