Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created January 28, 2018 22:16
Show Gist options
  • Save caglarorhan/87aeb743d16e4c2181440708eb1bba67 to your computer and use it in GitHub Desktop.
Save caglarorhan/87aeb743d16e4c2181440708eb1bba67 to your computer and use it in GitHub Desktop.
Between Two Sets - A fraction problem (from hackerrank.com)
/*
Original URL: https://www.hackerrank.com/challenges/between-two-sets/problem
Consider two sets of positive integers, and . We say that a positive integer, , is between sets and if the following conditions are satisfied:
All elements in are factors of .
is a factor of all elements in .
In other words, some is between and if that value of satisfies for every in and also satisfies for every in . For example, if and , then our possible values are and .
Given and , find and print the number of integers (i.e., possible 's) that are between the two sets.
Input Format
The first line contains two space-separated integers describing the respective values of (the number of elements in set ) and (the number of elements in set ).
The second line contains distinct space-separated integers describing .
The third line contains distinct space-separated integers describing .
Constraints
Output Format
Print the number of integers that are considered to be between and .
Sample Input
2 3
2 4
16 32 96
Sample Output
3
Explanation
There are three values between and :
:
All the elements in evenly divide .
evenly divides all the elements in .
:
All the elements in evenly divide .
evenly divides all the elements in .
:
All the elements in evenly divide .
evenly divides all the elements in .
Thus, we print as our answer.
*/
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function getTotalX(a, b) {
// Complete this function
var tot=0; var aX=[]; var bX=[];
for(var x=1; x<=100;x++){
var durum=true;
a.forEach(function(ai){
durum=(x%ai==0)*durum;
});
if(durum){aX.push(x)}
}
for(var x=1; x<=100;x++){
var durum=true;
b.forEach(function(bi){
durum=(bi%x==0)*durum;
});
if(durum){bX.push(x)}
}
//compare aX and bX
aX.forEach(function(aXi){
bX.forEach(function(bXi){
if(aXi==bXi){tot++}
});
});
return tot;
}
function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var m = parseInt(n_temp[1]);
a = readLine().split(' ');
a = a.map(Number);
b = readLine().split(' ');
b = b.map(Number);
var total = getTotalX(a, b);
process.stdout.write("" + total + "\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment