I found another really interesting problem on HackerRank called Between Two Sets. It is interesting because there was some math to be applied to the problem. The problem is a part of the implementation series on algorithms. The problem description is

Consider two sets of positive integers, and . We say that a positive integer, x, is

betweensets A and B if the following conditions are satisfied:

- All elements in
Aare factors ofx.xis a factor of all elements inB.In other words, some

xisbetweenAandBif that value ofxsatisfies for every inAand also satisfies for every inB. For example, if and , then our possiblexvalues are 6 and 12.Given

AandB, find and print the number of integers (i.e., possiblex‘s) that arebetweenthe two sets.

There are some constraints given to the between two sets. These constraints mean that it will be possible to make a bruteforce solution. We could test every number between 1 and 100 to see if they satisfy the condition for all numbers in both A and B. However, we can also look at a little bit of math to find some properties of the numbers we are testing. Continue reading →