3v4l.org

run code in 300+ PHP versions simultaneously
<?php function seqCheck(array $arr, int $maxMis): bool { $oos = 0; $mis = 0; foreach ($arr as $k => $v) { if ($k > 0 && $v < $arr[$k - 1]) { $oos++; // number out of sequence } if ($k > 0 && $v !== $arr[$k - 1] + 1) { $mis++; // number missing in sequence } } if ($oos > 0 || $mis > $maxMis) { return false; } else { return true; } } $tests = [ [[2, 3, 5, 6], 0], [[2, 3, 5, 6], 1], [[2, 5, 6, 8], 1], [[3, 6, 10, 16], 1], [[5, 6, 7, 8, 9, 10, 11], 1], [[1, 3, 5], 2], [[1, 5, 3], 2], ]; foreach ($tests as [$array, $timesForgivable]) { printf( "Test: %-17s,\tMax Forgiven: %d,\tOutcome: %s\n", json_encode($array), $timesForgivable, json_encode(seqCheck($array, $timesForgivable)) ); }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Test: [2,3,5,6] , Max Forgiven: 0, Outcome: false Test: [2,3,5,6] , Max Forgiven: 1, Outcome: true Test: [2,5,6,8] , Max Forgiven: 1, Outcome: false Test: [3,6,10,16] , Max Forgiven: 1, Outcome: false Test: [5,6,7,8,9,10,11], Max Forgiven: 1, Outcome: true Test: [1,3,5] , Max Forgiven: 2, Outcome: true Test: [1,5,3] , Max Forgiven: 2, Outcome: false

preferences:
64.93 ms | 407 KiB | 5 Q