0

I need to find the chronological next number. For example , I have an array :

$numbers = array(1,2,4);

I want function give me the missing chronological number : 3

more example :

$numbers = array(6,7,8,11);

need to give me : 9

Thanks

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
user1403317
  • 73
  • 2
  • 6
  • 1
    On the second array, why not 10? Also, what have you tried? This smells suspiciously of homework. – Madara's Ghost Oct 16 '12 at 17:51
  • Still, a challenge is a challenge! I'm 95% sure it's homework, but we all needed to learn somewhere. At least this person bothered to format the post instead of copy-pasting it, complete with corrupt MS Word characters. – Scott Oct 16 '12 at 17:53

3 Answers3

2

The only way I see doing this is to use sort on your array, then looping through the entire array.

To insert the number at the specified position in the array, look at this.

Use nickb's answer though - much better!

Community
  • 1
  • 1
Scott
  • 5,338
  • 5
  • 45
  • 70
2

The solution is simple once you make the assumption that the array is sorted with:

sort( $numbers);

Then, you can get the complete range of numbers, do a difference on the complete set and the input array, and take the first element (which will be the first element that differs):

Since the array is sorted, we can grab the min and max elements like this (we could also call min() and max()):

$min = current( $numbers);
$max = end( $numbers);

Now, we use range() to get the complete set of numbers, then call array_diff() to find the difference:

$complete = range( $min, $max);
$diff = array_diff( $complete, $numbers);

The first consecutive missing number is in the first element in the difference:

$first_missing = array_shift( $diff);

The benefit here is that $diff will contain all of the missing numbers between the range of $min and $max.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • Good point - but if you're getting the min and max of the array, why not just use that range to *replace* the array? EDIT: The OP was edited, not sure if he wants the array filled in or to get the first missing number. – Scott Oct 16 '12 at 17:57
  • 1
    @Jaxo - I think he is looking for the first missing number. If he wants the array filled it, this also does that with the `$complete` variable. `:)` – nickb Oct 16 '12 at 18:00
1

This should do the trick:

<?php
function chrono_trigger($array) {
    sort($array, SORT_NUMERIC);
    $prev = False;
    foreach($array as $num) {
        if($prev === False) {
            $prev = $num;
            continue;
        }
        if($prev != ($num-1)) {
            return $prev+1;
        }
        $prev = $num;
    }
    return False;
}

$numbers1 = array(1,2,4); // Should return 3
$numbers2 = array(6,7,8,11); // Should return 9
$numbers3 = array(1,2,3,4); // Shouldn't return anything

// Returns 3
$missing1 = chrono_trigger($numbers1);
var_export($missing1);

// Returns 9
$missing2 = chrono_trigger($numbers2);
var_export($missing2);

// Returns False
$missing3 = chrono_trigger($numbers3);
var_export($missing3);
Joshua Burns
  • 8,268
  • 4
  • 48
  • 61