PROBLEM DESCRIPTION
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.
SOLUTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int findGCD(int[] nums) {
int n = nums.length;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i=0; i<n; i++){
max = Math.max(max, nums[i]);
min = Math.min(min, nums[i]);
}
return GCD(max, min);
}
public int GCD(int a, int b){
if(b == 0) return a;
return GCD(b, a%b);
}
}