-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProblem662.java
55 lines (42 loc) · 1.16 KB
/
Problem662.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* This problem was asked by Amazon. Given n numbers, find the greatest common
* denominator between them. For example, given the numbers [42, 56, 14], return
* 14.
*
*/
public class Problem662 {
public static void main(String[] args) {
int gcd = gcd(new int[] { 42, 56, 14 });
assert gcd == 14;
gcd = gcd(new int[] { 13, 13 });
assert gcd == 13;
gcd = gcd(new int[] { 37, 600 });
assert gcd == 1;
gcd = gcd(new int[] { 624129, 2061517 });
assert gcd == 18913;
}
public static int gcd(int[] arr) {
int gcd = arr[0];
for (int i = 1; i < arr.length; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
public static int gcd(int a, int b) {
int gcd;
if (a == 0) {
gcd = b;
} else if (b == 0) {
gcd = a;
} else if (a == 1 || b == 1) {
return 1;
} else if (a == b) {
gcd = a; // or b, either - they're both same.
} else if (a > b) {
gcd = gcd(a % b, b);
} else {
gcd = gcd(a, b % a);
}
return gcd;
}
}