-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
41 lines (30 loc) · 867 Bytes
/
Solution.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
/**
* @author Oleg Cherednik
* @since 31.04.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(getGreatestCommonDenominator(new int[] { 42, 56, 14 })); // 14
}
public static int getGreatestCommonDenominator(int[] arr) {
int min = getMin(arr);
for (int i = 1, j = min / 2; i < j; i++) {
int k = min / i;
if (isAccepted(k, arr))
return k;
}
return Integer.MAX_VALUE;
}
private static int getMin(int[] arr) {
int min = Integer.MAX_VALUE;
for (int val : arr)
min = Math.min(min, val);
return min;
}
private static boolean isAccepted(int k, int[] arr) {
for (int val : arr)
if (val % k != 0)
return false;
return true;
}
}