HackerRank Between Two Sets Problem Solution
In this post, We are going to solve HackerRank Between Two Sets Problem.
There will be two arrays of integers. Determine all integers that satisfy the following two conditions:
- The elements of the first array are all factors of the integer being considered
- 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.
Function Description
Complete the getTotalX function in the editor below. It should return the number of integers that are between the sets.
get total x 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 space-separated integers a[I] where 0 < I < n.
The third line contains m distinct space-separated integers b[j] where 0 < j < m.
Sample Input
2 3
2 4
16 32 96
Sample Output
3
Explanation
2 and 4 divided evenly into 4, 8, 12, and 16.
4, 8, and 16 divided evenly into 16, 32, and 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 C Solution
#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;
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]);
}
// Find OBEB
int obeb = 1;
int factor = 2;
int maxfactor=1;
int i,j;
for(i=0;i<n;i++)
if(*(a+i)>maxfactor)
maxfactor = *(a+i);
while(factor<=maxfactor)
{
for(i=0;i<n;i++)
{
if(*(a+i)%factor == 0)
{
obeb *= factor;
*(a+i) /= factor;
for(j=i+1;j<n;j++)
{
if(*(a+j)%factor == 0)
{
*(a+j) /= factor;
}
}
maxfactor=1;
for(j=0;j<n;j++)
if(*(a+j)>maxfactor)
maxfactor = *(a+j);
factor--;
break;
}
}
factor++;
//printf("%d-%d\n",factor,maxfactor);
}
//printf("%d\n",obeb);
// Find OKEK
int okek = 1;
factor = 2;
maxfactor=1;
for(i=0;i<m;i++)
if(*(b+i)>maxfactor)
maxfactor = *(b+i);
while(factor<=maxfactor)
{
for(i=0;i<m;i++)
{
if(*(b+i)%factor != 0)
{
break;
}
}
if(i==m)
{
okek *= factor;
for(j=0;j<m;j++)
{
*(b+j) /= factor;
}
maxfactor=1;
for(j=0;j<m;j++)
if(*(b+j)>maxfactor)
maxfactor = *(b+j);
factor--;
}
factor++;
}
if(obeb==okek)
printf("1");
else if(okek%obeb!=0)
printf("0");
else
{
int k=2;
int count=2;
//printf("%d-%d\n",obeb,okek);
while(k*obeb<okek)
{
if(okek%(obeb*k)==0)
count++;
k++;
}
printf("%d",count);
}
return 0;
}
Between Two Sets C++ Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N], b[N];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
for (int i = 0; i < m; i++) {
scanf("%d", b + i);
}
int ans = 0;
for (int i = 1; i <= 1000; i++) {
bool ok = true;
for (int j = 0; j < n; j++) {
if (i % a[j] != 0) {
ok = false;
}
}
for (int j = 0; j < m; j++) {
if (b[j] % i != 0) {
ok = false;
}
}
if (ok) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
Between Two Sets C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
string[] tokens_n = Console.ReadLine().Split(' ');
Convert.ToInt32(tokens_n[0]);
Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
string[] b_temp = Console.ReadLine().Split(' ');
int[] b = Array.ConvertAll(b_temp,Int32.Parse);
var bFactors = GetAllFactors(b);
Console.WriteLine(CountFactorsOf(bFactors, a));
}
static int CountFactorsOf(IEnumerable<int> numbers, IEnumerable<int> testFactors){
var factors = new HashSet<int>();
foreach(int number in numbers){
if(testFactors.All(f=>number%f==0)){
//Console.WriteLine("{0} is common",number);
factors.Add(number);
}
}
return factors.Count();
}
static IEnumerable<int> GetAllFactors(IEnumerable<int> numbers){
var factors = new HashSet<int>();
for(int i=1;i<=numbers.Max();i++){
if(numbers.All(x=>x%i==0)){
//Console.WriteLine("{0} is a common factor",i);
factors.Add(i);
}
}
return factors;
}
}
Between Two Sets Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'getTotalX' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
private static int getHCF(int x, int y) {
if (x == 0 || y == 0) {
return x+y;
}
return getHCF(y, x%y);
}
private static int getLCM(int x, int y) {
return (x*y)/getHCF(x, y);
}
public static int getTotalX(List<Integer> a, List<Integer> b) {
// Write your code here
// find LCM of list a
// LCM(x, y) = (x * y) / HCF(x, y)
int lcmA = a.get(0);
for (int i = 0; i < a.size() - 1; i++) {
lcmA = getLCM(lcmA, a.get(i+1));
}
// find HCF of list b
int hcfB = b.get(0);
for (int i = 0; i < b.size() - 1; i++) {
hcfB = getHCF(hcfB, b.get(i+1));
}
// compute all multiples of LCM <= HCF vaue
int count = 0;
int id = 0;
int multiple = lcmA;
// System.out.println("HCF: " + hcfB);
// System.out.println("LCM: " + lcmA);
while (multiple <= hcfB) {
id++;
multiple = lcmA * id;
// System.out.println("Multiple: " + multiple);
if (hcfB % multiple == 0) {
// System.out.println("Increase count");
count++;
}
}
return count;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> brr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int total = Result.getTotalX(arr, brr);
bufferedWriter.write(String.valueOf(total));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Between Two Sets JavaScript Solution
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();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var m = parseInt(n_temp[1]);
a = readLine().split(' ');
a = a.map(Number).sort();
b = readLine().split(' ');
b = b.map(Number).sort();
var maxA = a.slice(-1)[0];
var minB = b[0];
var between = 0;
for(var num = maxA; num <= minB; num = num + maxA) {
var isFactored = a.every(function(aNum){
return num%aNum===0;
});
var isFactor = b.every(function(bNum){
return bNum%num===0;
});
if(isFactored && isFactor){
between++;
}
}
console.log(between);
}
Between Two Sets Python Solution
from itertools import *
from operator import *
from functools import *
n, m = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
print(sum([not any(map(partial(reduce, mod), list(product([x], A)) + list(product(B, [x])))) for x in range(1, 200)]))
Other Solutions