-
Notifications
You must be signed in to change notification settings - Fork 0
/
between_two_sets.js
50 lines (42 loc) · 1017 Bytes
/
between_two_sets.js
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
// https://www.hackerrank.com/challenges/between-two-sets/problem
function getTotalX(a, b) {
let lcm = a[0];
let gcd_n = b[0];
if (a.length > 1) {
for( let i = 0; i <a.length ; i++){
lcm = (lcm * a[i]) / gcd(lcm, a[i]) ;
}
}
if( b.length > 1){
for( let i = 0; i < b.length ; i++){
gcd_n = gcd(gcd_n, b[i]);
}
}
let count = 0;
for( let i = lcm; i < gcd_n+1; i+=lcm ){
if (gcd(i, gcd_n) == i){
count++;
}
}
return count;
function gcd(k, l) {
// Everything divides 0
if (k == 0 && l == 0){
return 0;
}
if (k == 0) {
return l;
}
if (l == 0)
return k;
// Base case
if (k == l) {
return k;
}
// k is greater
if (k > l) {
return gcd(k - l, l);
}
return gcd(k, l - k);
}
}