-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
66 lines (62 loc) · 1.82 KB
/
solution.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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class result {
// main function
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // the number of elements in array a
int m = in.nextInt(); // the number of elements in array b
int[] a = new int[n];
// taking elements of array a
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
// taking elements of array a
int[] b = new int[m];
for(int b_i=0; b_i < m; b_i++){
b[b_i] = in.nextInt();
}
// sort both arrays in increasing order
Arrays.sort(a);
Arrays.sort(b);
int min = a[0];
int max = b[b.length-1];
int count=0;
for(int i=min;i<=max;i++){
if(hasFactors(i,a) && isFactor(i,b)){
count++;
}
}
// number of integers that are between the sets
System.out.println(count);
}
// hasFactors function
public static boolean hasFactors(int num, int[] arr){
// check if elements of array are factor of num
for(int i=0;i<arr.length;i++){
if(num%arr[i]!=0){
return false;
}
}
return true;
}
// isFactor function
public static boolean isFactor(int num, int[] arr){
// check if your num is factor
for(int i=0;i<arr.length;i++){
if(arr[i]%num!=0){
return false;
}
}
return true;
}
}