Sort by

recency

|

3032 Discussions

|

  • + 0 comments

    my simple javascript code, dont be overengineering

    function getTotalX(a, b) {
        // Write your code here
        let count = 0
        
        for (let i=a[a.length - 1];i<=b[0];i++) {
            const isFactorB = b.every(v => v % i === 0)
            const isAFactor = a.every(v => i % v === 0)
            
            if (isFactorB && isAFactor) {
                count++
            }
        }
        
        return count
    }
    
  • + 0 comments
    def getTotalX(a, b):
        count=0
        bet_1=max(arr)
        bet_2=min(brr)
        
        for factor in range(bet_1, bet_2+1):
            if all(factor%i==0 for i in arr):
                if all(j%factor==0 for j in brr):
                    count+=1
        return count
    
  • + 1 comment

    Javascript Code:

    function getTotalX(a, b) {
        // Write your code here
        let maxValue = Math.max(...a);
        let minValue = Math.min(...b);
    
        let i = 2;
        let elements = [];
        let element = maxValue;
        while(element<=minValue){ 
            let factors = a.filter(num=>element%num == 0); 
            if(factors.length == a.length){ 
                let isFactor = b.filter(num1=>num1%element == 0);
                if(isFactor.length == b.length){
                    elements.push(element);
                }
            }
            element=maxValue*i; 
            i++; 
            
        }
        return elements.length;
    }
    
  • + 0 comments

    JAVA8 CODE public static int gcd(int x, int y){ if(y==0) return x; return gcd(y, x%y); } public static int lcm(int x, int y){ return x*y/gcd(x,y); }

    public static int getTotalX(List<Integer> a, List<Integer> b) {
    // Write your code here        
      int count =0;
      int Lcm = a.get(0);
      for(int i =0;i<a.size();i++){
        Lcm= lcm(Lcm,a.get(i));
      }
      int Gcd=b.get(0);
      for(int i =0;i<b.size();i++){
        Gcd=gcd(Gcd,b.get(i));
      }
      for(int i =Lcm;i<=Gcd;i+=Lcm){
        if(Gcd%i==0)
         count++;
      }
      return count;
    
    }
    

    }

    }
    

    }

  • + 0 comments

    For Javascript, this is my solution. I don't think this is optimal solution. Coz it is having O(n^2).

    function getTotalX(a, b) {
        // Write your code here
        a.sort((a,b)=>(a-b));
        b.sort((a,b)=>(a-b));
        let count = 0;
        let startVal = a[a.length - 1];
        let endVal = b[0];
        while (startVal <= endVal) {
            let firstArr = 0;
            let secArr = 0;
            for (let num of a) {
                if (startVal % num === 0) {
                    firstArr ++;
                }
            }
            for (let num of b) {
                if (num % startVal === 0) {
                    secArr ++;
                }
            }
            if (firstArr === a.length && secArr === b.length) {
                count ++;
            }
            startVal ++;
        }
        return count;
    }