Find Missing Number: Write a PHP function to find the missing number in an array of consecutive integers.

function findMissingNumber($arr) {
    $n = count($arr) + 1;
    $total = ($n * ($n + 1)) / 2;
    $sum = array_sum($arr);
    return $total - $sum;
}

// Example usage:
$array = [1, 2, 4, 5, 6];
echo "Missing number: " . findMissingNumber($array);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Artificial Intelligence