We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the getTotalX function below.
*/
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
static int getTotalX(int[] a, int[] b) {
//llcm of a
int num1 = a[0];
for (int i = 0; i < a.length; i++) {
num1 = lcm(a[i], num1);
}
///System.out.println(num1);
// Gcd of b
int num = b[0];
for (int i = 0; i < b.length; i++) {
num = gcd(b[i], num);
/// System.out.println(num);
}
int counter = 0;
for(int i=1; i<= num/num1; i++){
if(num % (num1*i)==0)
++counter;
}
return counter;
}
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nm = scan.nextLine().split(" ");
int n = Integer.parseInt(nm[0].trim());
int m = Integer.parseInt(nm[1].trim());
int[] a = new int[n];
String[] aItems = scan.nextLine().split(" ");
for (int aItr = 0; aItr < n; aItr++) {
int aItem = Integer.parseInt(aItems[aItr].trim());
a[aItr] = aItem;
}
int[] b = new int[m];
String[] bItems = scan.nextLine().split(" ");
for (int bItr = 0; bItr < m; bItr++) {
int bItem = Integer.parseInt(bItems[bItr].trim());
b[bItr] = bItem;
}
int total = getTotalX(a, b);
bw.write(String.valueOf(total));
bw.newLine();
bw.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Between Two Sets
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.math.; import java.text.; import java.util.; import java.util.regex.*;
public class Solution {
}