# HackerRank Between Two Sets Solution

#### ByBrokenprogrammers

Dec 11, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Between Two Sets Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Between Two Sets Solution

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

a = [2 ,6]
b = [24, 36]
There are two numbers between the arrays: 6 and 12.
6%2 = 0, 6%6 = 0, 24%6 = 0 and 36%6 = 0 for the first value.
12%2 = 0, 12%6 = 0 and 24%12 = 036%12 = 0 for the second value. Return 2.

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, n and m, the number of elements in arrays a and b.
The second line contains n distinct spaceseparated integers a[i] where 0 <= i < n.
The third line contains m distinct space-separated integers b[j] where 0 <= j < m.

Constraints

• 1 <= nm <= 10
• 1 <= a[i] <= 100
• 1 <= b[j] <= 100

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.

## HackerRank Between Two Sets Solution

### Between Two Sets Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n,i,min,max,j,c=0,f;
int m;
scanf("%d %d",&n,&m);
int *a = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
}
int *b = malloc(sizeof(int) * m);
for(int b_i = 0; b_i < m; b_i++){
scanf("%d",&b[b_i]);
}
max=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
min=b[0];
for(i=0;i<m;i++)
{
if(b[i]<min)
min=b[i];
}
for(i=max;i<=min;i++)
{
f=0;
for(j=0;j<n;j++)
{
if(i%a[j]!=0)
{
f=1;
break;
}
}
if(f==0)
{
for(j=0;j<m;j++)
{
if(b[j]%i!=0)
{
f=1;
break;
}
}
}
if(f==0)
c++;
}
printf("%d",c);
return 0;
}```

### Between Two Sets Solution in Cpp

```#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int checkA(const vector<int>& a, int n, int k){
for (int i = 0; i < n; i++)
if (k % a[i])
return 0;
return 1;
}

int checkB(const vector<int>& b, int n, int k){
for (int i = 0; i < n; i++)
if (b[i] % k)
return 0;
return 1;
}
int main(){
int n;
int m;
cin >> n >> m;
vector<int> a(n);
for(int a_i = 0;a_i < n;a_i++){
cin >> a[a_i];
}
vector<int> b(m);
for(int b_i = 0;b_i < m;b_i++){
cin >> b[b_i];
}
int cnt = 0;
for (int i = 1; i <= 100; i++)
if (checkA(a, n, i) && checkB(b, m, i))
cnt++;
cout << cnt;
return 0;
}```

### Between Two Sets Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] a = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int[] b = new int[m];
for(int b_i=0; b_i < m; b_i++){
b[b_i] = in.nextInt();
}

int ans = 0;

for(int j=1;j<=100;j++)
{
boolean hh = true;
for(int bb : b)
{
if(bb%j!=0)
{
hh = false;
break;
}
}

if(hh)
{
for(int bb : a)
{
if(j%bb!=0)
{
hh = false;
break;
}
}

if(hh)
ans++;

}
}

System.out.println(ans);
}
}```

### Between Two Sets Solution in Python

```#!/bin/python
import sys
n,m = raw_input().strip().split(' ')
n,m = [int(n),int(m)]
a = map(int,raw_input().strip().split(' '))
b = map(int,raw_input().strip().split(' '))
def are_factor(li, n):
return all(n%e == 0 for e in li)
def is_factor(li, n):
return all(e%n == 0 for e in li)
c = sum(are_factor(a, e) and is_factor(b, e) for e in range(1, 101))
print c```

### Between Two Sets Solution using JavaScript

```process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(n_temp[0]);
var m = parseInt(n_temp[1]);
var count = 0;
a = a.map(Number);
b = b.map(Number);
min = 1;
a.forEach(function(val,index){
if(val>min){min = val;};
});
max = 100;
b.forEach(function(val,index){
if(val<max){max = val;};
});
for(var i = min ; i<= max ; i++){
var result = true;
for(var ii = 0 ; ii < a.length ; ii++){
if(i%a[ii] !== 0){
result = false;
break;
}
}
if(result){
for(var ii = 0 ; ii < b.length ; ii++){
if(b[ii]%i !== 0){
result = false;
break;
}
}
}

if(result){
count++;
}
}
console.log(count);
}```

### Between Two Sets Solution in Scala

```object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
var m = sc.nextInt();
var a = new Array[Int](n);
for(a_i <- 0 to n-1) {
a(a_i) = sc.nextInt();
}
var b = new Array[Int](m);
for(b_i <- 0 to m-1) {
b(b_i) = sc.nextInt();
}

println((a.max to b.min).count(i => a.forall(j => i % j == 0) && b.forall(j => j % i == 0)))
}
}```

### Between Two Sets Solution in Pascal

```(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
var n,m,ans,j,i,x:longint;
a,b:array[1..100]of longint;
fl:boolean;
begin
for i:=1 to n do
for j:=1 to m do
for i:=1 to 100 do
begin
fl:=true;
for x:=1 to n do
if i mod a[x] >0
then begin fl :=false;break;end;
for x:=1 to m do
if b[x] mod i >0
then begin fl :=false;break;end;
if fl then inc(ans);
end;
write(ans);
end.```

Disclaimer: This problem (Between Two Sets) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.