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 solution Mini-Max sum in PHP

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. For example,  . Our minimum sum is   and our maximum sum is  . We would print 26 24 Function Description Complete the  miniMaxSum  function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of   of   elements. miniMaxSum has the following parameter(s): arr : an array of   integers Input Format A single line of five space-separated integers. Constraints Output Format Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly  four  of the five integers. (The output can be greater than a 32 bi...

PHP Hacker Rank Question "Time Conversion" Solution in php

Given a time in  -hour AM/PM format , convert it to military (24-hour) time. Note: Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock. Function Description Complete the  timeConversion  function in the editor below. It should return a new string representing the input time in 24 hour format. timeConversion has the following parameter(s): s : a string representing time in   hour format Input Format A single string   containing a time in  -hour clock format (i.e.:   or  ), where   and  . Constraints All input times are valid Output Format Convert and print the given time in  -hour format, where  . Solution : ---------------------------------- <?php /* Write your PHP code here */ $s = "12:45:54PM";  $t= $c = substr($s,0,2); $a = (int)$t; $sym = su...

PHP Hacker Rank "Grading Sturdents " Solution Php

HackerLand University has the following grading policy: Every student receives a   in the inclusive range from   to  . Any   less than   is a failing grade. Sam is a professor at the university and likes to round each student's   according to these rules: If the difference between the   and the next multiple of   is less than  , round   up to the next multiple of  . If the value of   is less than  , no rounding occurs as the result will still be a failing grade. For example,   will be rounded to   but   will not be rounded because the rounding would result in a number that is less than  . Given the initial value of   for each of Sam's   students, write code to automate the rounding process. Function Description Complete the function  gradingStudents  in the editor below. It should retu...