Sort by

recency

|

3072 Discussions

|

  • + 0 comments

    **Java Solution **

    public static int getTotalX(List<Integer> a, List<Integer> b) {
    
        /* Assuming the last value in 'a' is the maximum and
           the first value in 'b' is the minimum */
    
        int start = a.get(a.size()-1),end=b.get(0),count=0;
    
                for (int i = start; i <= end; i+=start) { 
    
                    boolean trueForA = true, trueForB = true ;
    
                    for (int num : a) {
                        if (i % num != 0) {
                            trueForA = false;
                            break;
                        }
                    }
    
                    for (int num : b) {
                        if(trueForA)
                            if (num % i != 0) {
                            trueForB = false;
                            break;
                        }
                      }
    
    
                    if (trueForA &&  trueForB) {
                       count++;
                     }
             }
        return count;
    }
    
  • + 0 comments

    THIS IS PYTHON CODE:

    import math
    import os
    import random
    import re
    import sys
    from functools import reduce
    
    def lcm(x, y):
        return x * y // math.gcd(x, y)
    
    def getTotalX(a, b):
        l = reduce(lcm, a)
        g = reduce(math.gcd, b)
        count = 0
        for i in range(l, g + 1, l):
            if g % i == 0:
                count += 1
        return count
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
        n = int(first_multiple_input[0])
        m = int(first_multiple_input[1])
    
        arr = list(map(int, input().rstrip().split()))
        brr = list(map(int, input().rstrip().split()))
    
        total = getTotalX(arr, brr)
    
        fptr.write(str(total) + '\n')
        fptr.close()
    
  • + 0 comments

    // Get the last element of set 'a' and the first element of set 'b' int startRange = a.get(a.size() - 1); Click Here int endRange = b.get(0);

    // You can now iterate over the range from startRange to endRange for (int i = startRange; i <= endRange; i++) { // Check if 'i' is divisible by all elements of 'a' and is a factor of all elements of 'b' boolean divisibleByAllA = true; boolean factorOfAllB = true;

    for (int num : a) {
        if (i % num != 0) {
            divisibleByAllA = false;
            break;
        }
    }
    
    for (int num : b) {
        if (num % i != 0) {
            factorOfAllB = false;
            break;
        }
    }
    
    if (divisibleByAllA && factorOfAllB) {
        // Your logic for valid integer within range
        System.out.println(i);
    }
    

    }

  • + 0 comments
    def getTotalX(a, b):
        count = 0
        for x in range(max(a), min(b)+1):
            if all(x % ai == 0 for ai in a) and all(bj % x == 0 for bj in b):
                count += 1
        return count
    
  • + 0 comments

    This solution is best using the brute-force approach — it's shorter, faster, and cleaner vs LCM and GCD traditional.

    def getTotalX(a, b):
        # Write your code here
        count = 0
        for x in range(max(a), min(b) + 1):
            # Check if x is divisible by all elements in a
            if all(x % ai == 0 for ai in a):
                # Check if x divides all elements in b
                if all(bi % x == 0 for bi in b):
                    count += 1
        return count