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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <iostream>
#include <vector>
using namespace std;

int getTotalX(vector<int> a, vector<int> b)
{
	//create new vector C
	int x = 0;
	bool flag = false;
	int lastItemInSet_A = a[a.size()-1];
	vector<int> 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<int> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	vector<int> 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  ///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; i<set1.size(); i++)
        if(set1[i]>largest) largest = set1[i];
        if(set1[i]<smallest)smallest = set[i];
      
      for(var i = 0; i<set1.size(); i++)
        if(set2[i]>largest) largest = set2[i];
        if(set2[i]<smallest)smallest = se2[i];
    
     ///your range is [smallest - largest];
     
     ///loop through the range see if any value meets the rules
     ///or you can define predicates for the rules and use std::all_of
     
      for(var X = smallest; X<=largest; X++)
       {
           auto pred1 =[X](int val){return X%val == 0;};//rule1
           auto pred2 = [X](int val){return val%X == 0;};//rule2
          
           if(std::all_of(set1.begin(),set1.end(),pred1) && std::all_of(set2.begin(),set2.end(),pred2))
      ///count X 
      
        }
       
  }
  
   //print you count
  


Topic archived. No new replies allowed.