-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathBetweenTwoSets.java
More file actions
51 lines (45 loc) · 1.06 KB
/
Copy pathBetweenTwoSets.java
File metadata and controls
51 lines (45 loc) · 1.06 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
/**
*
* Problem Statement-
* [Between Two Sets](https://www.hackerrank.com/challenges/between-two-sets/problem)
*
*/
package com.javaaid.hackerrank.solutions.generalprogramming.basicprogramming;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class BetweenTwoSets {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int gcd = 0;
int a[] = new int[n];
int b[] = new int[m];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int lcm = a[0];
for (int i = 0; i < m; i++) {
b[i] = sc.nextInt();
gcd = findGCD(b[i], gcd);
}
for (int i = 0; i < n - 1; i++) {
lcm = (lcm * a[i + 1]) / findGCD(a[i + 1], lcm);
}
int count = 0, t = 0;
for (int i = 1; i <= gcd && t <= gcd; i++) {
t = lcm * i;
if (gcd % (lcm * i) == 0) {
count++;
}
}
System.out.println(count);
sc.close();
}
private static int findGCD(int a, int b) {
return b == 0 ? a : findGCD(b, a % b);
}
}