Skip to content

Instantly share code, notes, and snippets.

@Neo-sunny
Created January 8, 2020 06:29
Show Gist options
  • Save Neo-sunny/ab12b9f25fddd2d132ae947b7b706e45 to your computer and use it in GitHub Desktop.
Save Neo-sunny/ab12b9f25fddd2d132ae947b7b706e45 to your computer and use it in GitHub Desktop.
public static int getTotalX(List<Integer> a, List<Integer> b) {
Collections.sort(a); Collections.sort(b);
int lcmOfa = lcmOfNumbers(a);
int gcdofb = b.get(0);
for(int i=1; i<b.size(); i++){
gcdofb = gcd(gcdofb,b.get(i));
}
int start = lcmOfa;
int end = gcdofb;
int count=0;
while(start<=end){
if(gcdofb%start==0){
count++;
}
start+=start;
}
return count;
}
static int lcmOfNumbers(List<Integer> a){
int lcm =1;
for(int i=0; i<a.size(); i++){
lcm = lcm*a.get(i)/gcd(lcm, a.get(i));
}
return lcm;
}
static int gcd(int a, int b){
if(b==0)
return a;
return gcd(b, a%b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment