How to Find the Missing Number in a Given Integer Array of 1 to 100 in PHP?
Last Updated :
27 Jun, 2024
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;
OutputThe 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;
OutputThe 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;
OutputThe 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;
OutputThe missing number is: 51
Similar Reads
Find the missing number in a sorted array of limited range Given a sorted array of size n, consisting of integers from 1 to n+1 with one missing. The task is to find the missing element.Examples: Input : arr[] = [1, 3, 4, 5, 6]Output : 2Input : arr[] = [1, 2, 3, 4, 5, 7, 8, 9, 10]Output : 6Input: arr[] = [1, 2, 3, 4]Output: 5Using Linear Search - O(n) time
11 min read
How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to
2 min read
Find all numbers in range [1, N] that are not present in given Array Given an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array. Examples: Input: arr[ ] = {5, 5, 4, 4, 2}Output: 1 3Explanation: For all numbers in the range [1, 5], 1 and 3 are
4 min read
Find the only missing number in a sorted array You are given a sorted array of N integers from 1 to N with one number missing find the missing number Examples: Input :ar[] = {1, 3, 4, 5}Output : 2Input : ar[] = {1, 2, 3, 4, 5, 7, 8}Output : 6A simple solution is to linearly traverse the given array. Find the point where current element is not on
9 min read
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read
Find all missing numbers from a given sorted array Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]]. Examples: Input: arr[] = {6, 7, 10, 11, 13}Output: 8 9 12 Explanation: The elements of the array are present in the range of the maximum and minimum array e
13 min read