Skip to main content

Hacker Rank "Between Tow Sets" solution

Question : -------

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
Solution :-----------
<?php
/* Write your PHP code here */
$a = array(2,4);
$b = array(16,32,96);
$y = 0;
$c = 0;
$arr = array();
$count = 0;
// checking if any number between 6 and 24 is divisable factor in array a;
for($i = $a[1]; $i<=$b[0]; $i++){
$len = count($a);
for($x=0;$x<$len;$x++){
if($i % $a[$x] == 0){
$y = $i;
}else{
$y = "";
break;
}
if($y != ""){
$arr[$c] = $y;
$c++;
}
}
// Now we have a array ($arr) where all the factors of array ($a) is stored.
// Now Divide array ($b) elements with $arr .
for($i=0;$i<count($arr);$i++){
for($j = 0;$j<count($b);$j++){
if($b[$j] % $arr[$i] == 0){
$z = $arr[$i];
}else{
$z = "";
break;
}
}
if($z != ""){
$count++;
}

}
echo $count; // Numbers of elements which are factors of the sets.
?>

Comments

Popular posts from this blog

PHP Hacker Rank Question "Birthday Cake Candles" solution !

You are in-charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out. For example, if your niece is turning   years old, and the cake will have   candles of height  ,  ,  ,  , she will be able to blow out   candles successfully, since the tallest candle is of height   and there are   such candles. Function Description Complete the function  birthdayCakeCandles  in the editor below. It must return an integer representing the number of candles she can blow out. birthdayCakeCandles has the following parameter(s): k : the integer threshold of students on time for class to continue a : an array of integers representing student arrival times Input Format The first line...

Hacker Rank "Division Sum pairs" solution

Question :--- You are given an array of   integers,  , and a positive integer,  . Find and print the number of   pairs where   and   +   is divisible by  . For example,   and  . Our three pairs meeting the criteria are   and  . Function Description Complete the  divisibleSumPairs  function in the editor below. It should return the integer count of pairs meeting the criteria. divisibleSumPairs has the following parameter(s): n : the integer length of array  ar : an array of integers k : the integer to divide the pair sum by Input Format The first line contains   space-separated integers,   and  . The second line contains   space-separated integers describing the values of  . Constraints Output Format Print the number of   pairs where   and   +  ...

Top 3 Most popular Front-end Frameworks

Each framework has its own strengths and weaknesses, and specific areas of application, allowing you to choose based on the needs of a specific project. For example, if your project is simple, there’s no need to use a complex framework. Also, many of the options are modular, allowing you to use only the components you need, or even mix components from different front-end frameworks. 1 . Bootstrap  Bootstrap  is the undisputed leader among the available front-end frameworks today. Given its huge popularity, which is still growing every day, you can be sure that this wonderful toolkit won’t fail you, or leave you alone on your way to building successful websites. 2. Foundation Foundation  is the second big player in this front-end framework comparison. With a solid company like  ZURB  backing it, this framework has a truly strong … well … foundation. After all, Foundation is used on many big websites including Facebook, Mozilla, Ebay, Yahoo! a...