Between Two Sets HackerRank Solution in C, C++, Java, Python

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=0,36%12=0  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,n  and m, the number of elements in arrays  and b.

The second line contains n distinct space-separated integers a[i] where 0<=i

The third line contains m distinct space-separated integers b[j] where 0<=j

Constraints

  • 1<=n,m<=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.

 

Between Two Sets HackerRank Solution in C

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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;imax)
max=a[i];
}
min=b[0];
for(i=0;i

 

 

Between Two Sets HackerRank Solution in C++

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int checkA(const vector& a, int n, int k){
for (int i = 0; i < n; i++)
if (k % a[i])
return 0;
return 1;
}

int checkB(const vector& 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 a(n);
for(int a_i = 0;a_i < n;a_i++){
cin >> a[a_i];
}
vector 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 HackerRank 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 HackerRank Solution in Python

aa,bb=0,0
n,m=map(int,raw_input().split())
a=map(int,raw_input().split())
b=map(int,raw_input().split())
ct=0
for i in xrange(max(a),min(b)+1):
for j in a:
if i%j!=0:
break
else:
for k in b:
if k%i!=0:
break
else:
ct+=1
print ct

 

Between Two Sets HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
class Solution {
static void Main(String[] args) {
Console.ReadLine();
var A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse).ToList();
var B = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse).ToList();

int c = 0;

for (int i = 1; i < 10000; i++) {
if (A.Any(x => i % x != 0)) continue;
if (B.Any(x => x % i != 0)) continue;
c++;
}
Console.WriteLine(c);
}
}

 

Attempt – Between Two Sets HackerRank Challenge

Link – https://www.hackerrank.com/challenges/between-two-sets

Next HackerRank Challenge Solution

Link – https://exploringbits.com/breaking-the-records-hackerrank-solution/

 

Leave a Comment