Skip to content
Advertisement

Problem in “between two set” hackerrank problem

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

The elements of the first array are all factors of the integer being considered 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 :Sample Input

JavaScript

Sample Output

JavaScript

My code:

JavaScript

My code is not giving proper solution and I cant understand why.

Advertisement

Answer

JavaScript

Great progress. You were very close. Algorithm is on point and efficient.

Only one mistake: you were resetting the variables x and y inside the if conditions.

What if the condition is not true? Then the variables are never reset and all the future computations are done on those wrong values in x and y.


Like Java8? Here is a one-liner:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement