We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

I found this outrageously difficult for being "easy" and I would have not even begun to understand what I was supposed to do without reading the discussions. "Factor" ? "Between two sets"? Why not call things by their name : GCD, LCM ?
Congratulations to those who solved this problem without help, this was way over my head.

Just like karayv said, you could bruteforce it by testing the condition against all the integers in a small range set by the inputs - like so:

intgetTotalX(vector<int>a,vector<int>b){// Range of int goes from the LAST element of A to the LAST of B// (some have said it should end on the FIRST of B, but I played it safely)intstart=a.back(),end=b.back();intcount=0;for(intx=start;x<=end;x++){// Booleans to test whether X fulfills conditionboola_pass=true,b_pass=true;for(constauto&ai:a){if(x%ai!=0){// If it doesn't, set flag and break out of A loopa_pass=false;break;}}// If A didn't complete successfully, continue with next integerif(!a_pass)continue;for(constauto&bi:b){if(bi%x!=0){// Idem A loopb_pass=false;break;}}// If both test were OK, count that integerif(a_pass&&b_pass)count++;}returncount;}

Arguably, the GCD/LCM approach is more efficient if done correctly (around O(n/log(n)) vs this O(n^2)), yet for such a small range it shouldn't be much of a problem.

I would say it was definitely challenging. Maybe the easy side of hard; not expert.

Although I thought I had it figured out early on, I got caught up as I always do, having to adjust for this, then having to adjust for that... and then, the edge cases. (case #3 taught me I missed testing the left (a) side power/mod while also testing the b-side factors.)

That's the problem with using problems like these in an interview. There's always a trick to them. I guarantee that no one in here devised the GCD/LCM method by themselves in their solutions. The algorithms were figured out over spans of decades by different mathematicians.

For those who are still trying to understand the question...

You run through some integers to check them against the arrays, whether they match the expecations as the two given. When they do, these integers become the answer.

So lets say we are investigating integer X. The integer X has to be

--> Perfectly divisible by all elements in the first array. If you divide X by any integer from the array, you would need to get a full number. This by definition means that the elements of such array would be factors of X. Such condition can be checked by the MOD function, which returns the remainder of division, where X % Array1Element = 0

--> Perfectly divide into all elements in the second array. Each element divided by X would produce an integer result. Such that Array2Element % X = 0

## Between Two Sets

You are viewing a single comment's thread. Return to all comments →

I found this outrageously difficult for being "easy" and I would have not even begun to understand what I was supposed to do without reading the discussions. "Factor" ? "Between two sets"? Why not call things by their name : GCD, LCM ?

Congratulations to those who solved this problem without help, this was way over my head.

Considewring the boundaries you can solve the task with simple brudeforce solution. Guys are just having fun here with GCD and LCM.

Just like karayv said, you could bruteforce it by testing the condition against all the integers in a small range set by the inputs - like so:

Arguably, the GCD/LCM approach is more efficient if done correctly (around

O(vs thisn/log(n))O(), yet for such a small range it shouldn't be much of a problem.n^2)I would say it was definitely challenging. Maybe the easy side of hard; not expert.

Although I thought I had it figured out early on, I got caught up as I always do, having to adjust for this, then having to adjust for that... and then, the edge cases. (case #3 taught me I missed testing the left (a) side power/mod while also testing the b-side factors.)

That's the problem with using problems like these in an interview. There's always a trick to them. I guarantee that no one in here devised the GCD/LCM method by themselves in their solutions. The algorithms were figured out over spans of decades by different mathematicians.

For those who are still trying to understand the question...

You run through some integers to check them against the arrays, whether they match the expecations as the two given. When they do, these integers become the answer.

So lets say we are investigating integer X. The integer X has to be

--> Perfectly divisible by all elements in the first array. If you divide X by any integer from the array, you would need to get a full number. This by definition means that the elements of such array would be factors of X. Such condition can be checked by the MOD function, which returns the remainder of division, where

X % Array1Element = 0--> Perfectly divide into all elements in the second array. Each element divided by X would produce an integer result. Such that

Array2Element % X = 0