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.

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.

## Between Two Sets

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

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)