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.
Between Two Sets
Between Two Sets
+ 0 comments def getTotalX(a, b): # Write your code here max_a=max(a) min_b=min(b) count=0 for i in range(max_a, min_b+1): if all(i %item==0 for item in a): if all(item %i==0 for item in b): count+=1 return count
+ 0 comments class Result { public static int getTotalX(List<Integer> a, List<Integer> b) { int lcmA = listLcm(a); int upper = Collections.min(b); if(lcmA<=0){ return 0; } if(lcmA>upper){ return 0; } int count=0; for(int i=lcmA;i<=upper;i++){ if(aCompat(i, a) && bCompat(i, b)){ count++; } } return count; } static int gcd(int x, int y){ int temp=y; if(x<y){ y=x; x=temp; } while(y!=0){ temp=y; if(y==0) return x; y=x%y; x=temp; } return x; } static int lcm(int x, int y){ return (x*y)/gcd(x,y); } static int listLcm(List<Integer> a){ int result=1; for(int i=0;i<a.size();i++){ result = lcm(result, a.get(i)); } return result; } public static boolean aCompat(int num, List<Integer> a){ for(int i=0;i<a.size();i++){ if(num%a.get(i)!=0){ return false; } } return true; } public static boolean bCompat(int num, List<Integer> b){ for(int i=0;i<b.size();i++){ if(b.get(i)%num!=0){ return false; } } return true; } }
+ 0 comments Python 3
def getTotalX(a, b): counter = 0 min_x, max_x = max(a), min(b) lis1 = [x for x in range(min_x, max_x+1) if all(x % value == 0 for value in a)] lis2 = [x for x in lis1 if all(value % x == 0 for value in b)] return len(lis2)
+ 0 comments Java 8 import java.util.List; import java.util.NoSuchElementException;
class Result {
public static int getTotalX(List<Integer> a, List<Integer> b) { // Write your code here int minA = a.stream().max(Integer::compareTo).orElseThrow(NoSuchElementException::new); int maxB = b.stream().min(Integer::compareTo).orElseThrow(NoSuchElementException::new); int counter = 0; for (int i = minA; i <= maxB; i++) { if (isArrayElementsFactors(a, i) && isFactorOfAllElements(i, b)) { counter++; } } return counter; } public static boolean isArrayElementsFactors(List<Integer> a, int number) { for (int el : a) { if (number % el != 0) return false; } return true; } public static boolean isFactorOfAllElements(int number, List<Integer> b) { for (int el : b) { if (el % number != 0) return false; } return true; }
}
we get the minimum number in the max number in the first array and the minimum number in the second one, because 1- if the number isn't the max in first array it can't be factor of the max number. 2-if the number isn't the min in second array it can't be factor of the minimum number in the array.
then we made the 2 help functions to check the conditions.
+ 0 comments JS version
function getTotalX(a, b) { let result = 0; for (let i = Math.max(...a); i <= Math.min(...b); i++) { let isFactorMultiple = true; for (let ele of a) { if (i % ele !== 0) { isFactorMultiple = false; break; } } if (isFactorMultiple) { for (let ele of b) { if (ele % i !== 0) { isFactorMultiple = false; break; } } } if (isFactorMultiple) { result++; } } return result; }
Load more conversations
Sort 2805 Discussions, By:
Please Login in order to post a comment