Last active
March 22, 2019 14:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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