<?php
$a = array(1, 2, 4, 6, 8, 9, 10, 15, 16, 17, 20, 21, 23, 24, 26, 27, 28, 29, 31, 39);
$maiorDistancia = 0;
$sequencias = [];
$ultimaSeq = 0;
for ($i = 0; $i < count($a)-1; ++$i){ $dist = abs($a[$i]-$a[$i+1]);
if ($dist > $maiorDistancia) $maiorDistancia = $dist;
if (($a[$i+1]-$a[$i]) == 1){
if (isset($sequencias[$ultimaSeq])){ $sequencias[$ultimaSeq][] = $a[$i+1];
}
else {
$sequencias[$ultimaSeq][0] = $a[$i];
$sequencias[$ultimaSeq][1] = $a[$i+1];
}
}
else {
$ultimaSeq++;
}
}
echo ("Maior distancia: $maiorDistancia \n Sequencias: ");
PD9waHAKJGEgPSBhcnJheSgxLCAyLCA0LCA2LCA4LCA5LCAxMCwgMTUsIDE2LCAxNywgMjAsIDIxLCAyMywgMjQsIDI2LCAyNywgMjgsIDI5LCAzMSwgMzkpOwoKJG1haW9yRGlzdGFuY2lhID0gMDsKJHNlcXVlbmNpYXMgPSBbXTsKJHVsdGltYVNlcSA9IDA7Cgpmb3IgKCRpID0gMDsgJGkgPCBjb3VudCgkYSktMTsgKyskaSl7CiAgICAkZGlzdCA9IGFicygkYVskaV0tJGFbJGkrMV0pOwogICAgCiAgICBpZiAoJGRpc3QgPiAkbWFpb3JEaXN0YW5jaWEpICRtYWlvckRpc3RhbmNpYSA9ICRkaXN0OwogICAgCiAgICBpZiAoKCRhWyRpKzFdLSRhWyRpXSkgPT0gMSl7CiAgICAgICAgaWYgKGlzc2V0KCRzZXF1ZW5jaWFzWyR1bHRpbWFTZXFdKSl7CiAgICAgICAgICAgICRzZXF1ZW5jaWFzWyR1bHRpbWFTZXFdW10gPSAkYVskaSsxXTsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICRzZXF1ZW5jaWFzWyR1bHRpbWFTZXFdWzBdID0gJGFbJGldOwogICAgICAgICAgICAkc2VxdWVuY2lhc1skdWx0aW1hU2VxXVsxXSA9ICRhWyRpKzFdOwogICAgICAgIH0KICAgIH0KICAgIGVsc2UgewogICAgICAgICR1bHRpbWFTZXErKzsKICAgIH0KfQoKZWNobyAoIk1haW9yIGRpc3RhbmNpYTogJG1haW9yRGlzdGFuY2lhIFxuIFNlcXVlbmNpYXM6ICIpOwoKcHJpbnRfcigkc2VxdWVuY2lhcyk7