-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbetween-two-sets.cpp
More file actions
65 lines (51 loc) · 1.5 KB
/
between-two-sets.cpp
File metadata and controls
65 lines (51 loc) · 1.5 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
challenge https://www.hackerrank.com/challenges/between-two-sets/problem
This needed gcd and lcm. I made a big code. Was getting late and so went to discussions. My code was easier to read but some testcases
couldn't pass. I will drink poison while saying i copied the code. Anyway the good learnt was about auto datatype.
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int gcd(int num1, int num2) {
auto divisor = min(num1, num2);
for(; divisor > 0; --divisor)
if ((num1 % divisor == 0) && (num2 % divisor == 0))
break;
return divisor;
}
int gcd_element(const vector<int>& v)
{
return accumulate(begin(v), end(v), *begin(v), gcd);
}
int lcm(int num1, int num2)
{
return (num1 * num2) / gcd(num1, num2);
}
int lcm_element(const vector<int>& v)
{
return accumulate(begin(v), end(v), *begin(v), lcm);
}
int is_divisible(int num1, int num2)
{
return num1 % num2 == 0 ? 1 : 0;
}
int main() {
int m, n;
cin >> m >> n;
vector<int> A(m);
vector<int> B(n);
copy_n(istream_iterator<int>(cin), m, begin(A));
copy_n(istream_iterator<int>(cin), n, begin(B));
auto lcmOfA = lcm_element(A);
auto gcdOfB = gcd_element(B);
auto counter = 0;
auto multipleOfLcm = lcmOfA;
while (multipleOfLcm <= gcdOfB)
{
counter += is_divisible(gcdOfB, multipleOfLcm);
multipleOfLcm += lcmOfA;
}
cout << counter << endl;
return 0;
}