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.
I found the problem super easy and I feel like this is a good solution using Java. Does anyone know if I missed something or messed up the time complexity? It appears to be O((m+n)*i)
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int maxA = 0;
int minB = 101;
int[] a = new int[n];
for(int a_i=0; a_i < n; a_i++){
int tmp = in.nextInt();
maxA = tmp > maxA ? tmp:maxA;
a[a_i] = tmp;
}
int[] b = new int[m];
for(int b_i=0; b_i < m; b_i++){
int tmp = in.nextInt();
minB = tmp < minB ? tmp:minB;
b[b_i] = tmp;
}
int integersBetween = 0;
intCheck:
for(int i = maxA; i <= minB; i += maxA)
{
//Check if all A are a factor of i
for(int num : a)
{
if(i%num != 0)
{
continue intCheck;
}
}
//Check if i is a factor of all B
for(int num : b)
{
if(num%i != 0)
{
continue intCheck;
}
}
integersBetween++;
}
System.out.println(integersBetween);
}
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 →
I found the problem super easy and I feel like this is a good solution using Java. Does anyone know if I missed something or messed up the time complexity? It appears to be O((m+n)*i)