Skip to content

Instantly share code, notes, and snippets.

@anujkaushal
Last active March 22, 2019 14:31
Show Gist options
  • Save anujkaushal/f7ae8a2195cc92bb983b309ae7035587 to your computer and use it in GitHub Desktop.
Save anujkaushal/f7ae8a2195cc92bb983b309ae7035587 to your computer and use it in GitHub Desktop.
Write a algorithm/program to find the smallest positive number missing from the array
<?php
function findnumber($numbers)
{
$count = count($numbers);
for ($i = 1; $i <= $count; $i++)
{
$flag = true;
for ($j = 0; $j < $count; $j++)
{
if($numbers[$j] <= 0) continue;
if($i == $numbers[$j]){
$flag = false;
break;
}
}
if($flag){
return $i;
}
}
}
$numbers = array(12, 7, 28, -2, 30, 5, 33, -8, 1, 2);
print "for reference ";
print_r($numbers);
print "\nsmallest +ve missing number is ";
echo findnumber($numbers);
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment