-
Notifications
You must be signed in to change notification settings - Fork 1
/
BetweenTwoSets.cpp
90 lines (63 loc) · 1.19 KB
/
BetweenTwoSets.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// problem: "https://www.hackerrank.com/challenges/between-two-sets/problem"
#include<iostream>
using namespace std;
int HCF (int,int);
int LCM (int,int);
int main()
{
long int hcf,lcm,count,n,m,A[10],B[10],big,small;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>A[i];
for(int i=0;i<m;i++)
cin>>B[i];
big=A[0];
for(int i=1;i<n;i++)
if(big<A[i])
big=A[i];
small=B[0];
for(int i=0;i<m;i++)
if(small>B[i])
small=B[i];
if(big>small)
count=0;
else
{
count =0;
lcm=A[0];
for(int i=0;i<n-1;i++)
{
lcm=LCM(lcm,A[i+1]);
}
hcf=B[0];
for(int j=0;j<m-1;j++)
{
hcf=HCF(hcf,B[j+1]);
}
int k=1;
while(lcm*k<=hcf)
{
if(hcf%(lcm*k)==0)
count++;
k++;
}
}
cout<<count;
return 0;
}
int HCF (int a,int b)
{
if(a!=b)
{
if(a>b)
return HCF(a-b,b);
else
return HCF(b-a,a);
}
else
return a;
}
int LCM (int a, int b)
{
return (a*b/HCF(a,b));
}