'Solution for Missing integer problem of codility in php 100% score
what is the best implementation in PHP for missing integer problem of codility?
Solution below results 66%, causing performance issue.
function solution($A)
{
sort($A);
$end = count($A);
$flag = false;
for ($k = 0; $flag == false; $k++, $flag = false) {
for ($i = 0; $i < $end; $i++) {
if ($k + 1 == $A[$i]) {
$flag = $A[$i];
break;
}
}
if($flag == false){
return $k +1;
}
}
}
Solution 1:[1]
Here is the Best solution for the codility problem implemented in PHP, scoring 100%
function solution($A)
{
sort($A);
$end = count($A);
$flag = false;
for ($k = 1, $i = 0; $i < $end; $i++) {
if ($A[$i] == $k) {
$k++;
continue;
} elseif ($A[$i] < $k)
continue;
else return $k;
}
return $k;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nabeel Perwaiz |
