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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Between Two Sets
  5. Discussions

Between Two Sets

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2619 Discussions, By:

recency

Please Login in order to post a comment

  • ahmetmelikaksoy
    3 days ago+ 0 comments
    def getTotalX(a, b):
        m = len(a)
        n = len(b)
        f_list=[]
        
        #try all numbers up to min(b) and add those that divide elements of b to the f_list    
        for i in range(1,min(b)+1):
            for j in range(n):
                flag = 1
                if b[j]%i!=0:
                    flag = 0
                    break
            if flag and i>=min(a):
                f_list.append(i)
                
        #remove those that are not divisible by elements of a             
        for i in tuple(f_list):
            for j in range(m):
                if i%a[j] != 0:
                    f_list.remove(i)
                    break
        return len(f_list)
    
    0|
    Permalink
  • srishtiverma1306
    4 days ago+ 0 comments

    //Solution

    int getTotalX(vector a, vector b) { int n = a.size(); int m = b.size();

    //setting the number to be checked to be 'between two arrays' as maximum element from array a
    int x = *max_element(a.begin(), a.end());
    int count = 0;
    
    //iterators to check if x satisfies given conditions for all elements of array a and b
    int ia = 0, ib = 0;
    
    //maximum allowed value of a is minimum element of array b, for it to be a factor of elements of b
    while(x <= *min_element(b.begin(),b.end())){
        for(int i = 0; i < n; i++){
            if(x%a[i] == 0) ia++;
        }
        for(int i = 0; i < m; i++){
            if(b[i]%x == 0) ib++;
        }
        if(ia == n && ib == m) count++;
                //resetting values of iterators to check for next value of x
        ia = 0; ib = 0;                                      
        x++;
    }   
    
    return count;
    

    }

    0|
    Permalink
  • suhninwai120
    5 days ago+ 0 comments

    Java Euclidean Algorithm

    public static int getTotalX(List<Integer> a, List<Integer> b) {
        // Write your code here
        int gcdNumber = Collections.max(b);
        int lcmNumber = Collections.max(a);
        int total = 0;
            for(int i = 0; i < b.size(); i++){
                gcdNumber = gcd(gcdNumber, b.get(i));
            }
            for(int i = 0; i < a.size(); i++){
                lcmNumber = lcm(lcmNumber,a.get(i)); 
            }
            System.out.println(gcdNumber);
            System.out.println(lcmNumber);
            if( gcdNumber % lcmNumber == 0){
                int count = 1;
                while(gcdNumber>=(lcmNumber*count)){
                    if( (gcdNumber % (lcmNumber * count)) == 0){
                        total++;
                    }
                    count++;
                }
            }
            System.out.println(total);
            return total;
    
        }
        static int gcd(int a, int b)
        {
            if (a == 0)
                return b;
            return gcd(b % a, a);
        }
        
        // function to calculate
        // lcm of two numbers.
        static int lcm(int a, int b)
        {
            return (a * b) / gcd(a, b);
        }
    
    0|
    Permalink
  • gaikwadjayprasad
    7 days ago+ 0 comments

    In Java 8

    public static int getTotalX(List<Integer> a, List<Integer> b) {
        // Write your code here
        a.sort(Collections.reverseOrder());
        b.sort(Collections.reverseOrder());    
        boolean flag = true;    
        int counter = 0;
            for(int num=a.get(0);num<=b.get(b.size()-1);num++){                
                flag = true;
                for(int aValue : a){
                    if(num % aValue !=0)
                        flag = false;            
                }
                
                if(flag)
                    for(int bValue : b){
                        if(bValue % num !=0)
                            flag = false;
                    }
                
                if(flag)
                    counter++;
            }
            return counter;
        }
    
    -2|
    Permalink
  • favm72
    7 days ago+ 0 comments
    def getTotalX(a, b):
        lcma = math.lcm(*a)
        gcdb = math.gcd(*b)    
        if gcdb % lcma != 0:
            return 0    
        factors = [i*lcma for i in range(1, gcdb // lcma+1) if gcdb % (i*lcma) == 0]   
        return len(factors)
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy