HackerRank : Week of Code 25 – Between Two Sets

By | November 1, 2016

Consider two sets of positive integers, A and B . We say that a positive integer, x , is between sets and if the following conditions are satisfied:

  1. All elements in A are factors of  x.
  2. x is a factor of all elements in B.

Given A and B, find and print the number of integers (i.e., possible x‘s) that are between the two sets.

Input Format

The first line contains two space-separated integers describing the respective values of  n (the number of elements in set A) and m  (the number of elements in set B).
The second line contains n distinct space-separated integers .
The third line contains m distinct space-separated integers .

Constraints

 1<=a’s elements , b’s elements , n, m <=100

Output Format

Print the number of integers that are considered to be between A and B.

Sample Input

2 3
2 4
16 32 96

Sample Output

3

Explanation

The integers that are between A={2,4 } and B={16,32,96}are  4,8 , and 16 .

Solution

I have used two utility functions. One of which accepts an integer and an array and 
checks if all the elements of array are factor of the element.
The second function also takes an integer and an array as inputs and checks if the 
element is factorial of each element of the array.

I have run a loop from max of array A to min of array B and checked if the elements
is passing for both utility functions, if yes then I am increasing the count.

Count is the final variable here which is being printed in the end.