-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetweenTwoSets.cpp
More file actions
65 lines (54 loc) · 1.97 KB
/
BetweenTwoSets.cpp
File metadata and controls
65 lines (54 loc) · 1.97 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
/*
https://www.hackerrank.com/challenges/between-two-sets/problem
Between two sets problem.
Simply copy my implementation and paste it instead of
the provided function at the link above.
Long story short, find the number of elements between two sets that are GCDs
in the first array and LCMs in the second one.
Return the final count.
*/
int getTotalX(vector<int> a, vector<int> b) {
int final_count = 0;
// the last element of the first array is the start of the range we are looking at
int lower_bound = a.back();
// the first element of the second array is the end of the range we are looking at
int upper_bound = b.front();
// considerall elements in the range, inclusive
for(int i = lower_bound; i <= upper_bound; i++)
{
// update flags each loop
bool flagA = true;
bool flagB = true;
// loop through the first array to see if elements of the first array are divisors of i
for(int j = 0; j < a.size(); j++){
{
if(i % a[j] != 0)
{
// as soon as the first inconsistent number is found, break out of the loop, this is not the number
flagA = false;
break;
}
}
}
// no need to continue working with i if the first condition failed
if(!flagA)
continue;
// consider if i is a factor of each element in the second array
for(int j = 0; j < b.size(); j++)
{
{
if(b[j] % i != 0)
{
// break out of the loop if not a factor
flagB = false;
break;
}
}
}
// update the count only if both conditions are satisfied, i.e., lcm & gcd
if(flagA && flagB)
final_count++;
}
// return the final count of lcm & gcd numbers after breaking out of the big loop
return final_count;
}