Open In App

How to Find the Missing Number in a Given Integer Array of 1 to 100 in PHP?

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Finding the missing number in an integer array ranging from 1 to 100 is a common algorithmic problem. In PHP, this can be efficiently achieved by leveraging the arithmetic properties of the series. The sum of numbers from 1 to 100 is known, and by comparing this expected sum with the actual sum of the array elements, the missing number can be identified. This guide will demonstrate a straightforward approach to solving this problem using PHP.

These are the following approaches:

Using the sum of an arithmetic progression

Here, we are going to calculate the expected sum using the formula nĂ—(n+1)/2 and then calculate the actual sum using the function array_sum($arr). Then, we can subtract the actual sum from the expected sum and get the missing number.

Example: This example shows the use of sum of an arithmetic progression to find the given number in a given integer array.

PHP
<?php
function findMissingNumber($arr)
{
    $n = 100;
    $expectedSum = ($n * ($n + 1)) / 2;
    $arraySum = array_sum($arr);
    $missingNumber = $expectedSum - $arraySum;
    return $missingNumber;
}

$arr = range(1, 100);
unset($arr[50]);
$missingNumber = findMissingNumber($arr);
echo "The missing number is: " . $missingNumber;

Output
The missing number is: 51

Using the XOR (bitwise exclusive OR) operation

We'll use the property i.e. a ^ b ^ a = (a ^ a) ^ b = 0 ^ b = b. Using this property, we are going to XOR all the array elements and the numbers from 1 to 100, from which the missing number will be the result.

Example: This example shows the use of XOR (bitwise exclusive OR) operation to find the given number in a given integer array.

PHP
<?php
function findMissingNumber($arr)
{
    $n = 100;
    $missingNumber = 0;


    for ($i = 1; $i <= $n; $i++) {
        $missingNumber ^= $i;
    }

    foreach ($arr as $num) {
        $missingNumber ^= $num;
    }

    return $missingNumber;
}

$arr = range(1, 100);
unset($arr[50]);
$missingNumber = findMissingNumber($arr);
echo "The missing number is: " . $missingNumber;

Output
The missing number is: 51

Using the array_diff() function

We will compare two arrays and return the values from the first array that are not present or missing in the second array.

Example: This example shows the use of array_diff() function to find the given number in a given integer array.

PHP
<?php
function findMissingNumber($arr)
{
    $n = 100;
    $expectedArray = range(1, $n);
    $missingNumber = array_diff($expectedArray, $arr);
    return array_pop($missingNumber);
}

$arr = range(1, 100);
unset($arr[50]);
$missingNumber = findMissingNumber($arr);
echo "The missing number is: " . $missingNumber;

Output
The missing number is: 51

Using the array_sum() and array_fill() methods

We will create an array with all values from 1 to 100 using the range() function, then use array_values() function and reset the array keys to consecutive integers. Then we let it to search for the missing number using the function called array_search() in PHP.

Example: This example shows the use of array_sum() and array_fill() methods to find the given number in a given integer array.

PHP
<?php
function findMissingNumber($arr)
{
    $n = 100;
    $expectedArray = array_values(range(1, $n));
    $missingNumber = array_search(
        null,
        array_map(function ($value) use ($arr) {
            return in_array($value, $arr) ? $value : null;
        }, $expectedArray)
    );

    return $missingNumber !== false ? $missingNumber + 1 : null;
}

$arr = range(1, 100);
unset($arr[50]);
$missingNumber = findMissingNumber($arr);
echo "The missing number is: " . $missingNumber;

Output
The missing number is: 51

Next Article

Similar Reads