Between Two Sets (HackerRank)

Joshua Smith
Nov 5 · 3 min read

You will be given two arrays of integers and asked to determine all integers that satisfy the following two conditions:

  1. The elements of the first array are all factors of the integer being considered
  2. The integer being considered is a factor of all elements of the second array

These numbers are referred to as being between the two arrays. You must determine how many such numbers exist.

For example, given the arrays a =[2, 6] and b = [24, 36], there are two numbers between them: 6 and 12 . 6 % 2 = 0 , 6 % 6 = 0 , 24 % 6 = 0 and 36 % 6 = 0 for the first value. Similarly 12 % 6 = 0 and 24 % 12 = 0 , 36 % 12 = 0.

Function Description

Complete the getTotalX function in the editor below. It should return the number of integers that are between the sets.

getTotalX has the following parameter(s):

  • a: an array of integers
  • b: an array of integers

Input Format

The first line contains two space-separated integers, n, and m, the number of elements in the array a and the number of elements in the array b.
The second line contains n distinct space-separated integers describing a[i] where 0 ≤ i <n.
The third line contains distinct space-separated integers describing b[j] where 0 ≤ j < m.

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

2 and 4 divide evenly into 4, 8, 12 and 16.
4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.

SOLUTION

function getTotalX(a, b) {// Write your code hereconst lowLimit = a[a.length — 1];const highLimit = b[0];let consideredMultiples = [];//get multiples of the highest member in the first array till the lowest value in the second arrayfor (let i = lowLimit; i <= highLimit; i += lowLimit){consideredMultiples.push(i);}//filter the array based on whether members of first array can go in itconsideredMultiples = consideredMultiples.filter(multiple => {return a.reduce((shouldFilter, val) => {if (multiple % val !== 0){return false;}else if (shouldFilter){return true}}, true);});//filter the array based on whether its members can go in the second arrayconsideredMultiples = consideredMultiples.filter(multiple => {return b.reduce((shouldFilter, val) => {if (val % multiple !== 0){return false;}else if (shouldFilter){return true}}, true);});return consideredMultiples.length;}

SHORTER VERSION

function getTotalX(a, b) {// Write your code hereconst lowLimit = a[a.length — 1];const highLimit = b[0];let consideredMultiples = [];//get multiples of the highest member in the first array till the lowest value in the second arrayfor (let i = lowLimit; i <= highLimit; i += lowLimit){    consideredMultiples.push(i);}//filter the array based on whether members of first array can go in itconsideredMultiples = consideredMultiples.filter(multiple => a.reduce((shouldFilter, val) => (multiple % val !== 0)? false : (shouldFilter)? true: null , true));//filter the array based on whether its members can go in the second arrayconsideredMultiples = consideredMultiples.filter(multiple => b.reduce((shouldFilter, val) => (val % multiple !== 0)? false : (shouldFilter)? true: null , true));return consideredMultiples.length;}
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade