## 1979. 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`

#### Solution:

#### min() function explanation:

first we have initialized min variable= nums[0]; so it will store first value of array as minimum value after that I have created simple for loop to iterate all elements from array. while iterating nums array one by one it will check if(min>nums[i]) we are updating min value as min=nums[i] so new min value will be updated. after iterating all element we will get min value from array. so returning it as min.

similarly,

#### max() function explanation:

first we have initialized max variable= nums[0]; so it will store first value of array as maximum value after that I have created simple for loop to iterate all elements from array. while iterating nums array one by one it will check if(min<nums[i]) we are updating min value as max=nums[i] so new min value will be updated. after iterating all element we will get max value from array. so returning it as max.

#### public int findGCD(int[] nums):

findGCD() function- First we have created local variable ans and i have initialized with 0. after that we are checking a condition if(max(nums)%min(nums)==0) we are returning min(nums). it means that if from the array if max value is divisible by min value then answer will be minimus value of array otherwise we have to find max value which can divide min and max also. so for finding max value we are using for loop, it should be run from i=1 to i<min(nums). we checking if i will divide both minimus and maximum value we are just updating greated value using ans=i; so at the end of loop we will have greated value which can divide minimum and maximum value of array so we havbe just returning ans.

public int findGCD(int[] nums) {

int ans=0;

if(max(nums)%min(nums)==0)

{

return min(nums);

}

else

{

for(int i=1; i<min(nums); i++)

{

if(min(nums)%i==0 && max(nums)%i==0)

{

ans=i;

}

}

return ans;

}

}

public int min(int[] nums)

{

int min=nums[0];

for(int i=0; i<nums.length; i++)

{

if(min>nums[i])

{

min=nums[i];

}

}

return min;

}

public int max(int[] nums)

{

int max=nums[0];

for(int i=0; i<nums.length; i++)

{

if(max<nums[i])

{

max=nums[i];

}

}

return max;

}

}

## 0 Comments