-
Notifications
You must be signed in to change notification settings - Fork 5
/
CountNonDivisible.php
45 lines (36 loc) · 947 Bytes
/
CountNonDivisible.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* CountNonDivisible
*
* Calculate the number of elements of an array that are not divisors of each element.
*/
include '../../Tests.class.php';
function solution($A)
{
$sizeOfA = sizeof($A);
$maxValue = max($A);
$occurrence = array_fill(0, $maxValue+1, 0);
foreach ($A as $value) {
$occurrence[$value]++;
}
for ($i = 0; $i < $sizeOfA; $i++) {
$count = 0;
$j = 1;
while ($j * $j <= $A[$i]) {
if ($A[$i] % $j == 0) {
$count += $occurrence[$j];
if ($A[$i] / $j != $j) {
$count += $occurrence[$A[$i] / $j];
}
}
$j++;
}
$nonDivisibles[$i] = $sizeOfA - $count;
}
return $nonDivisibles;
}
$test = new Tests('CountNonDivisible');
$name = 'example';
$A = array(3, 1, 2, 3, 6);
$result = array(2, 4, 3, 2, 0);
$test->run(array($A), $result, $name);