Between Two Sets HackerRank Problem Solution

In this Between Two Sets HackerRank Problem, There will be two arrays of integers. Determine all integers that satisfy the following two conditions:

  1. The elements of the first array are all factors of the integer being considered
  2. The integer being considered is a factor of all elements of the second array

These numbers are referred to as being between the two arrays. Determine how many such numbers exist.

Example

There are two numbers between the arrays:  and .
, ,  and  for the first value.
,  and ,  for the second value. Return .

Function Description

Complete the getTotalX function in the editor below. It should return the number of integers that are betwen the sets.

getTotalX has the following parameter(s):

  • int a[n]: an array of integers
  • int b[m]: an array of integers

Returns

  • int: the number of integers that are between the sets

Input Format

The first line contains two space-separated integers,  and , the number of elements in arrays  and .
The second line contains  distinct space-separated integers  where .
The third line contains  distinct space-separated integers  where .

Constraints

Sample Input

2 3
2 4
16 32 96

Sample Output

3

Explanation

2 and 4 divide evenly into 4, 8, 12 and 16.
4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.

Between Two Sets HackerRank Problem Solutions

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in Python

def getTotalX(a, b):
    count = 0
    for i in range(a[-1], b[0]+1):
        flag = 1
        for j in range(0, len(a)):
            if i % a[j] != 0:
                flag = 0
                break
        if flag == 1:
            for j in range(0, len(b)):
                if b[j] % i != 0:
                    flag = 0
                    break
        if flag == 1:
            count += 1
    return(count)

Between Two Sets Problem Solution in C++

#include <bits/stdc++.h>


using namespace std;


int gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
}


int lcm(int a, int b){
    return a * b / gcd(a,b);
}


int main() {
    int n, m, e, l = 1, g = 0;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> e;
        l = lcm(l, e);
    }
    for(int i = 0; i < m; i++){
        cin >> e;
        g = gcd(g, e);
    }
    int res = 0;
    for(int c = l; c <= g; c+=l)if(g % c == 0) res ++;
    cout << res;
    return 0;
}

Between Two Sets Problem Solution in JavaScript

function getTotalX(a, b) {
    let validCount = 0;
   
    for (let x = 1; x <= 100; x++) {
        if (a.every(int => (x % int == 0))) {
            if (b.every(int => (int % x == 0))) {
                validCount++;
            }
        }
    }


    return validCount;
}

Solve original Problem on HackerRank here. Checkout more HackerRank Problems

1 thought on “Between Two Sets HackerRank Problem Solution”

Leave a Comment