Last active
May 25, 2023 04:36
-
-
Save allucardster/ae4d702104f100ad8bf58969f4a12334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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