Finding missing number from series This code will accept user input from the command line.

The first user can enter the limit of series.

e.g: user may enter 5

After that, the user will be prompted for 5 numbers of a series.

e.g:

2
4
8
10
12

Here in this series, 6 is missing, so I am going to find the missing series from common difference series.

I got the correct output from this code. Is it the correct way?  Is there any other simple code to do the same?

&lt;?php
    fscanf(STDIN, "%d\n", $count);
    $series = array();
    $common_diff = array();
    $prev_element = null;
    for($i=0;$i&lt;$count;$i++){
        fscanf(STDIN, "%d\n", $series[$i]);
        if($prev_element != null){
            $common_diff[] = $series[$i] - $prev_element;
        }
        $prev_element = $series[$i];
    }
    $c = array_count_values($common_diff);
    asort($c);
    end($c);
    $common_d = key($c);
    $prev_element = $series[0];
    $missed_no = 0;
    for($i=1;$i&lt;$count;$i++){
        $diff = $series[$i] - $prev_element;
        if($diff != $common_d){
            $missed_no = $prev_element + $common_d;
        }
        $prev_element = $series[$i];
    }
    echo $missed_no;
    exit;
?&gt;


example input

5
2
4
8
10
12
Functions

The code can be more readable if it is broken into functions.  This also separates the concern of reading in the series and calculating the missing element(s).

function readSeries($fileHandle)
{
    $series = array();
    fscanf($fileHandle, "%d\n", $count);

    for ($i = 0; $i &lt; $count; ++$i)
    {
        fscanf($fileHandle, "%d\n", $series[$i]);
    }

    return $series;
}

function findMissing(Array $series)
{
    sort($series);
    $start = reset($series);
    $finish = end($series);
    $step = ($finish - $start) / count($series);    

    return array_diff(range($start, $finish, $step),
                      $series);
}

$series = readSeries(STDIN);
$missing = findMissing($series);
echo reset($missing);


The use of functions also help to make the code more reusable. The readSeries function can easily be used to read from standard input or another source.

The findMissing function takes advantage of some assumptions that can be made when a series is sorted:


It would be impossible to notice whether the beginning or end elements were missed, so they must both be present.
One the beginning and end values are known then every step along this range must be present.


It then uses the difference of the expected series calculated using the PHP range function.