public int solution(int[] arrayA, int[] arrayB) {
int gcdA = calculateGCD(arrayA);
int gcdB = calculateGCD(arrayB);
gcdA = updateGCD(gcdA, arrayB);
gcdB = updateGCD(gcdB, arrayA);
if (gcdA == gcdB) {
return 0;
} else {
return Math.max(gcdA, gcdB);
}
}
public int calculateGCD(int[] arr) {
int gcd = arr[0];
for (int i = 1; i < arr.length; i++) {
gcd = calculateGCD(gcd, arr[i]);
}
return gcd;
}
public int calculateGCD(int a, int b) {
if (a == 0) {
return b;
}
return calculateGCD(b % a, a);
}
public int updateGCD(int gcd, int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] % gcd == 0) {
gcd = 1;
break;
}
}
return gcd;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/135807