Between Two Sets

You will be given two arrays of integers and asked to 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. You must determine how many such numbers exist.
For example, given the arrays  and , there are two numbers between them:  and  and  for the first value. Similarly,  and .
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):
  • a: an array of integers
  • b: an array of integers
Input Format
The first line contains two space-separated integers,  and , the number of elements in array  and the number of elements in array .
The second line contains  distinct space-separated integers describing  where .
The third line contains  distinct space-separated integers describing  where .
Constraints
Output Format
Print the number of integers that are considered to be between  and .
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.

 php
<?php

/*
* 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
*/
function lcmAr($arr) {
$lcm_val = $arr[0];
foreach($arr as $val) {
$lcm = gmp_lcm($lcm_val, $val);
$lcm_val = gmp_strval($lcm);
}
return $lcm_val;
}

function gcdAr($arr) {
$gcd_val = $arr[0];
foreach($arr as $val) {
$gcd = gmp_gcd($gcd_val, $val);
$gcd_val = gmp_strval($gcd);
}
return $gcd_val;
}


function getTotalX($a, $b) {
$lcmAr = lcmAr($a);
$gcdAr = gcdAr($b);
$cond = true;
$index = 0;
$counter = 0;
while($cond) {
$index++;
if($lcmAr > $gcdAr) {
$cond = false;
} else if($index * $lcmAr > $gcdAr ) {
$cond = false;
} else
{
$a =$index * $lcmAr;
if( $gcdAr % $a == 0 ) {
$counter++;
}
}
}

return $counter;
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$first_multiple_input = explode(' ', rtrim(fgets(STDIN)));

$n = intval($first_multiple_input[0]);

$m = intval($first_multiple_input[1]);

$arr_temp = rtrim(fgets(STDIN));

$arr = array_map('intval',
preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));

$brr_temp = rtrim(fgets(STDIN));

$brr = array_map('intval',
preg_split('/ /', $brr_temp, -1, PREG_SPLIT_NO_EMPTY));

$total = getTotalX($arr, $brr);

fwrite($fptr, $total . "\n");

fclose($fptr);

Comments

Popular posts from this blog

Intro to Tutorial Challenges

Strong Password