### Between Two Sets

I am trying to solve a problem but I got stuck somewhere.

This is a link to the problem: https://www.hackerrank.com/challenges/between-two-sets/problem

My idea was to create a new set in which I store the last element in A + all numbers between + first element in B.

Then I got stuck. I still don't know how to place my loops in a way that would make my x value correct.

This is my code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` #include #include using namespace std; int getTotalX(vector a, vector b) { //create new vector C int x = 0; bool flag = false; int lastItemInSet_A = a[a.size()-1]; vector c(b[0] - a[a.size() - 1] + 1); //this foor loop stores all numbers between the two sets A and B in the vector C. for (int j = 0; j < c.size(); j++) { c[j] = lastItemInSet_A; lastItemInSet_A++; } //this is where I got stuck. How do I do the logic here where I check if that X satisfies the conditions of the problem. for (int i = 0; i < c.size(); i++) { for (int z = 0; z < a.size(); z++) { if (c[i] % a[z] == 0) { cout << c[i] << " "; } } } return x; } int main() { int n; //size of a int m; //size of b cin >> n >> m; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector b(m); for (int j = 0; j < m; j++) { cin >> b[j]; } int x = getTotalX(a, b); //cout << x << endl; system("PAUSE"); return 0; }``````
hello there, this is how i understand the problem:

assuming you have all the input needed then you will:

A. check if all constraints are met
- these constraints are listed at the bottom of the exercise
- if any of the constraints are broken , get new data.
- you can define a function to check the constrains.

B. define the candidates for 'X'
- note that the candidates for 'x' can only fall between the
smallest and the largest values of both sets of data.

if A={2,4} and B={8,16,32}
the candidates of 'x' from these sets are between 2 and 32
hence range[2 - 32] defines all possible candidates.

C. counting your successful candidates x.
- after you have the range you should loop through it
examining if the candidate meets the predefined requirements
1. candidate % ai == 0; and
2. bi % candidate == 0;

if the candidate passed both tests, you can store or count it.
- to check this conditions you could use @Keskiverto's solution
which is quite simple or you can also use loops

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` `````` ///pseudo-code like function get_data(); bool constrains_met(); function get_total(); //get data do { get_data() } while(!constraints_met()); function get_total() { ///get x candidates variable smallest= set1[0],largest=set1[0]; for(var i = 0; ilargest) largest = set1[i]; if(set1[i]largest) largest = set2[i]; if(set2[i]

Topic archived. No new replies allowed.