Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created May 6, 2015 05:04
Show Gist options
  • Save stephen-maina/460ae73ebe1d289b7225 to your computer and use it in GitHub Desktop.
Save stephen-maina/460ae73ebe1d289b7225 to your computer and use it in GitHub Desktop.
did some online research
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
int count=0;
for(int index=0;index<B.length;index++){
int gcd=0;
int extra=0;
if(A[index]==0||B[index]==0){
continue;
}
if(A[index]>B[index]){
gcd=gcd(A[index],B[index]);
}else{
gcd=gcd(B[index],A[index]);
}
if(commonGCD(gcd,A[index])&&commonGCD(gcd,B[index])){
count++;
}
}
return count;
}
public int gcd(int N,int M){
if(N%M==0){
return M;
}else{
return gcd(M,N%M);
}
}
private boolean commonGCD(int x, int y) {
int d = gcd(x, y);
while (d != 1) {
y /= d;
d = gcd(x, y);
}
return x % y == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment