Skip to content

Instantly share code, notes, and snippets.

@allucardster
Last active May 25, 2023 04:36
Show Gist options
  • Save allucardster/ae4d702104f100ad8bf58969f4a12334 to your computer and use it in GitHub Desktop.
Save allucardster/ae4d702104f100ad8bf58969f4a12334 to your computer and use it in GitHub Desktop.
<?php
/*
Find the missing number in a range (array)
*/
function permMissingElem(array $arr) : int
{
$n = count($arr) + 1;
$num = (($n + 1) * $n) / 2;
foreach($arr as $val) {
$num = $num - $val;
}
return $num;
}
$test = [2, 3, 1, 5]; // 4
var_dump(permMissingElem($test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment