forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
53 lines (47 loc) · 1.38 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int getTotalX(int[] a, int[] b, int maxA, int minB) {
int counter = 0;
for (int i = maxA; i <= minB; i++) {
boolean sentinel = false;
for (int j = 0; j < a.length; j++) {
if (i % a[j] != 0) {
sentinel = true;
break;
}
}
for (int j = 0; j < b.length; j++) {
if (b[j] % i != 0) {
sentinel = true;
break;
}
}
if (!sentinel) { counter++; }
}
return counter;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int maxA = Integer.MIN_VALUE;
int minB = Integer.MAX_VALUE;
int[] a = new int[n];
for(int a_i = 0; a_i < n; a_i++){
a[a_i] = in.nextInt();
maxA = Math.max(maxA, a[a_i]);
}
int[] b = new int[m];
for(int b_i = 0; b_i < m; b_i++){
b[b_i] = in.nextInt();
minB = Math.min(minB, b[b_i]);
}
int total = getTotalX(a, b, maxA, minB);
System.out.println(total);
in.close();
}
}