Hi All,
Here, I have finding the Missing number from the array.
Function: sort();
Syntax: sort(array, sorttype);
Description: sorts an indexed array in ascending order.
Function: range();
Syntax: range(low, high, step);
Description : It creates an array containing a range of elements.
This function returns an array of elements from low to high.
Function: max();
Syntax: max(array_values);
or
max(value1,value2,...);
Description : It returns the highest value in an array, or the highest value of several specified values.
<?php
function missing_number($num_order) {
// Sort an array number for finding the first value of an array
sort($num_order);
// construct a new array
$new_array = range($num_order[0],max($num_order));
// use array_diff to find the missing elements
return array_diff($new_array , $num_order);
}
print_r(missing_number(array(10,4,3,6,7,8)));
echo"</br>";
print_r(missing_number(array(10,15,18,16,17,20)));
echo"</br>";
print_r(missing_number(array(1,2,5,6,7,8,9)));
echo"</br>";
?>
Output:
Array ( [2] => 5 [6] => 9 )
Array ( [1] => 11 [2] => 12 [3] => 13 [4] => 14 [9] => 19 )
Array ( [2] => 3 [3] => 4 )