fork(1) download
  1. <?php
  2. $a = array(1, 2, 4, 6, 8, 9, 10, 15, 16, 17, 20, 21, 23, 24, 26, 27, 28, 29, 31, 39);
  3.  
  4. $maiorDistancia = 0;
  5. $sequencias = [];
  6. $ultimaSeq = 0;
  7.  
  8. for ($i = 0; $i < count($a)-1; ++$i){
  9. $dist = abs($a[$i]-$a[$i+1]);
  10.  
  11. if ($dist > $maiorDistancia) $maiorDistancia = $dist;
  12.  
  13. if (($a[$i+1]-$a[$i]) == 1){
  14. if (isset($sequencias[$ultimaSeq])){
  15. $sequencias[$ultimaSeq][] = $a[$i+1];
  16. }
  17. else {
  18. $sequencias[$ultimaSeq][0] = $a[$i];
  19. $sequencias[$ultimaSeq][1] = $a[$i+1];
  20. }
  21. }
  22. else {
  23. $ultimaSeq++;
  24. }
  25. }
  26.  
  27. echo ("Maior distancia: $maiorDistancia \n Sequencias: ");
  28.  
  29. print_r($sequencias);
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Maior distancia: 8 
 Sequencias: Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [3] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 10
        )

    [4] => Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
        )

    [5] => Array
        (
            [0] => 20
            [1] => 21
        )

    [6] => Array
        (
            [0] => 23
            [1] => 24
        )

    [7] => Array
        (
            [0] => 26
            [1] => 27
            [2] => 28
            [3] => 29
        )

)