Sort by

recency

|

3093 Discussions

|

  • + 0 comments

    Python 3 Solution, this isn't the most optimal code, but it gets the job done. You can use it as a reference, though I wouldn't recommend using it in practice. It's still better to prioritize finding the Least Common Multiple (LCM) and the Greatest Common Divisor (GCD)

    def getTotalX(a, b):
        print(a, b)
        max_num  = max(a)
        min_num = min(b)
        result_a = []
        result_b = []
        for x in range(max_num, min_num + 1):
            flag = []
            for i in a:
                if  x % i == 0:
                    flag.append(True)
                else:
                    flag.append(False)
            if all(flag):
                result_a.append(x)
        
        for x in result_a:
            flag = []
            for i in b:
                if i % x == 0:
                    flag.append(True)
                else:
                    flag.append(False)
            if all(flag):
                result_b.append(x)
        return len(result_b)
    
  • + 0 comments

    Python solution

    def getTotalX(a, b): # Write your code here x = max(a) y = min(b)

    ans = []
    
    for i in range(x, y + 1):
        count = 0
        count2 = 0
    
        for j in a:
            if i % j == 0:
                count += 1
        if count == len(a):
            for m in b:
                if m % i == 0:
                    count2 += 1
            if count2 == len(b):
                ans.append(i)
    return len(ans)
    
  • + 1 comment

    I like that it’s designed to cater to all skill levels, so anyone can understand and use it without confusion. Clear learning systems like this, similar to alphabet in morse code, make complex ideas much easier to grasp.

  • + 0 comments

    Was stuck with the problem as I was using int for input and return format in sub-function, but fixed it after using long. This is one of the correct code:

    public static int getTotalX(List<int> a, List<int> b)
        {
            long lcmA = a[0];
            foreach (var num in a)
            {
                lcmA = LCM(num, lcmA);
                if (lcmA > int.MaxValue) return 0;
            }
            
            long gcdB = b[0];
            foreach (var num in b)
                gcdB = GCD(gcdB, num);
            
            int count = 0;
            for (long x = lcmA; x <= gcdB; x+=lcmA)
            {
                if (gcdB % x == 0)
                    count++;
            }
            
            return count;
        }
        
        private static long GCD(long a, long b)
        {
            while (b != 0)
            {
                long remainder = a % b;
                a = b;
                b = remainder;
            }
            
            return a;
        }
        
        private static long LCM(long a, long b)
        {
            return (a * b) / GCD(a, b);
        }
    
  • + 0 comments

    Most Optmized code: O(A)+O(B)+O((a_lcm/b_gcd)×B)

    def check_factors(n, arr):
        for i in arr:
            if i%n !=0:
                return False
        return True
    
    def getTotalX(a, b):
        # Write your code here
        factor = []
        a_lcm = math.lcm(*a)
        b_gcd = math.gcd(*b)
        for i in range(a_lcm,b_gcd+1,a_lcm):
            if check_factors(i,b):
                factor.append(i)
            
        return len(factor)
                
    

    `