-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBetweenTwoSets.java
89 lines (67 loc) · 2.1 KB
/
BetweenTwoSets.java
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
package implementation;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by tatarJR on 3/10/2017.
*/
public class BetweenTwoSets {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
for (int a_i = 0; a_i < n; a_i++) {
a[a_i] = in.nextInt();
}
int[] b = new int[m];
for (int b_i = 0; b_i < m; b_i++) {
b[b_i] = in.nextInt();
}
long startTime = System.currentTimeMillis();
Arrays.sort(a);
Arrays.sort(b);
int max = 0;
int min = 0;
if (a[n - 1] > b[m - 1]) {
max = a[n - 1];
} else {
max = b[m - 1];
}
if (a[0] > b[0]) {
min = b[0];
} else {
min = a[0];
}
ArrayList<Integer> numbersBetweenSets = new ArrayList<>();
for (int i = min; i <= max; i++) {
int factorCount = 0;
for (int j = 0; j < n; j++) {
if (i >= a[j] && (i % a[j] == 0)) {
factorCount++;
if ((!numbersBetweenSets.contains(i)) && factorCount == n) {
numbersBetweenSets.add(i);
}
}
}
}
int betweenSetsCount = 0;
for (int i : numbersBetweenSets) {
int factorCount = 0;
for (int k = 0; k < m; k++) {
if (b[k] >= i && (b[k] % i == 0)) {
factorCount++;
if (factorCount == m) {
betweenSetsCount++;
}
}
}
}
System.out.println(betweenSetsCount);
long endTime = System.currentTimeMillis();
NumberFormat formatter = new DecimalFormat("#0.00000");
System.out.print("Execution time is " + formatter.format((endTime - startTime) / 1000d) + " seconds");
}
}