You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in the list. One of the integers is missing in the list. Write an efficient code to find the missing integer. Example:
Input: arr[] = {1, 2, 4, 6, 3, 7, 8}
Output: 5
Explanation: The missing number from 1 to 8 is 5
Input: arr[] = {1, 2, 3, 5}
Output: 4
Explanation: The missing number from 1 to 5 is 4
Method 1: This method uses the technique of the summation formula.
Approach: The length of the array is n-1. So the sum of all n elements, i.e sum of numbers from 1 to n can be calculated using the formula n*(n+1)/2. Now find the sum of all the elements in the array and subtract it from the sum of first n natural numbers, it will be the value of the missing element.
Algorithm:
Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
Create a variable sum to store the sum of array elements.
Traverse the array from start to end.
Update the value of sum as sum = sum + array[i]
Print the missing number as sumtotal – sum
Implementation:
C++
filter_none
edit close
play_arrow
link brightness_4 code
#include <bits/stdc++.h>
usingnamespacestd;
// Function to get the missing number
intgetMissingNo(inta[], intn)
{
inttotal = (n + 1) * (n + 2) / 2;
for(inti = 0; i < n; i++)
total -= a[i];
returntotal;
}
// Driver Code
intmain()
{
intarr[] = { 1, 2, 4, 5, 6 };
intn = sizeof(arr) / sizeof(arr[0]);
intmiss = getMissingNo(arr, n);
cout << miss;
}
chevron_right
filter_none
C
filter_none
edit close
play_arrow
link brightness_4 code
#include <stdio.h>
/* getMissingNo takes array and size of array as arguments*/
intgetMissingNo(inta[], intn)
{
inti, total;
total = (n + 1) * (n + 2) / 2;
for(i = 0; i < n; i++)
total -= a[i];
returntotal;
}
/*program to test above function */
intmain()
{
inta[] = { 1, 2, 4, 5, 6 };
intmiss = getMissingNo(a, 5);
printf("%d", miss);
getchar();
}
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to find missing Number
classMain {
// Function to ind missing number
staticintgetMissingNo(inta[], intn)
{
inti, total;
total = (n + 1) * (n + 2) / 2;
for(i = 0; i < n; i++)
total -= a[i];
returntotal;
}
/* program to test above function */
publicstaticvoidmain(String args[])
{
inta[] = { 1, 2, 4, 5, 6};
intmiss = getMissingNo(a, 5);
System.out.println(miss);
}
}
chevron_right
filter_none
Python
filter_none
edit close
play_arrow
link brightness_4 code
# getMissingNo takes list as argument
defgetMissingNo(A):
n =len(A)
total =(n +1)*(n +2)/2
sum_of_A =sum(A)
returntotal -sum_of_A
# Driver program to test the above function
A =[1, 2, 4, 5, 6]
miss =getMissingNo(A)
print(miss)
# This code is contributed by Pratik Chhajer
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// C# program to find missing Number
usingSystem;
classGFG {
// Function to ind missing number
staticintgetMissingNo(int[] a, intn)
{
inttotal = (n + 1) * (n + 2) / 2;
for(inti = 0; i < n; i++)
total -= a[i];
returntotal;
}
/* program to test above function */
publicstaticvoidMain()
{
int[] a = { 1, 2, 4, 5, 6 };
intmiss = getMissingNo(a, 5);
Console.Write(miss);
}
}
// This code is contributed by Sam007_
chevron_right
filter_none
PHP
filter_none
edit close
play_arrow
link brightness_4 code
<?php
// PHP program to find
// the Missing Number
// getMissingNo takes array and
// size of array as arguments
functiongetMissingNo ($a, $n)
{
$total= ($n+ 1) * ($n+ 2) / 2;
for( $i= 0; $i< $n; $i++)
$total-= $a[$i];
return$total;
}
// Driver Code
$a= array(1, 2, 4, 5, 6);
$miss= getMissingNo($a, 5);
echo($miss);
// This code is contributed by Ajit.
?>
chevron_right
filter_none
Output:
3
Complexity Analysis:
Time Complexity: O(n). Only one traversal of the array is needed.
Space Complexity: O(1). No extra space is needed
Modification for Overflow
Approach: The approach remains the same but there can be overflow if n is large. In order to avoid integer overflow, pick one number from known numbers and subtract one number from given numbers. This way there won’t have Integer Overflow ever.
Algorithm:
Create a variable sum = 1 to which will store the missing number and a counter c = 2.
Traverse the array from start to end.
Update the value of sum as sum = sum – array[i] + c and update c as c++.
Time Complexity: O(n). Only one traversal of the array is needed.
Space Complexity:O(1). No extra space is needed
Thanks to Sahil Rally for suggesting this improvement. Method 2: This method uses the technique of XOR to solve the problem.
Approach: XOR has certain properties
Assume a1 ^ a2 ^ a3 ^ …^ an = a and a1 ^ a2 ^ a3 ^ …^ an-1 = b
Then a ^ b = an
Algorithm:
Create two variables a = 0 and b = 0
Run a loop from 1 to n with i as counter.
For every index update a as a = a ^ i
Now traverse the array from start to end.
For every index update b as b = b ^ array[i]
Print the missing number as a ^ b.
Implementation:
C++
filter_none
edit close
play_arrow
link brightness_4 code
#include <bits/stdc++.h>
usingnamespacestd;
// Function to get the missing number
intgetMissingNo(inta[], intn)
{
// For xor of all the elements in array
intx1 = a[0];
// For xor of all the elements from 1 to n+1
intx2 = 1;
for(inti = 1; i < n; i++)
x1 = x1 ^ a[i];
for(inti = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return(x1 ^ x2);
}
// Driver Code
intmain()
{
intarr[] = { 1, 2, 4, 5, 6 };
intn = sizeof(arr) / sizeof(arr[0]);
intmiss = getMissingNo(arr, n);
cout << miss;
}
chevron_right
filter_none
C
filter_none
edit close
play_arrow
link brightness_4 code
#include <stdio.h>
/* getMissingNo takes array and size of array as arguments*/
intgetMissingNo(inta[], intn)
{
inti;
intx1 = a[0]; /* For xor of all the elements in array */
intx2 = 1; /* For xor of all the elements from 1 to n+1 */
for(i = 1; i < n; i++)
x1 = x1 ^ a[i];
for(i = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return(x1 ^ x2);
}
/*program to test above function */
intmain()
{
inta[] = { 1, 2, 4, 5, 6 };
intmiss = getMissingNo(a, 5);
printf("%d", miss);
getchar();
}
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to find missing Number
// using xor
classMain {
// Function to find missing number
staticintgetMissingNo(inta[], intn)
{
intx1 = a[0];
intx2 = 1;
/* For xor of all the elements
in array */
for(inti = 1; i < n; i++)
x1 = x1 ^ a[i];
/* For xor of all the elements
from 1 to n+1 */
for(inti = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return(x1 ^ x2);
}
/* program to test above function */
publicstaticvoidmain(String args[])
{
inta[] = { 1, 2, 4, 5, 6};
intmiss = getMissingNo(a, 5);
System.out.println(miss);
}
}
chevron_right
filter_none
Python3
filter_none
edit close
play_arrow
link brightness_4 code
# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
defgetMissingNo(a, n):
x1 =a[0]
x2 =1
fori inrange(1, n):
x1 =x1 ^ a[i]
fori inrange(2, n +2):
x2 =x2 ^ i
returnx1 ^ x2
# Driver program to test above function
if__name__=='__main__':
a =[1, 2, 4, 5, 6]
n =len(a)
miss =getMissingNo(a, n)
print(miss)
# This code is contributed by Yatin Gupta
chevron_right
filter_none
C#
filter_none
edit close
play_arrow
link brightness_4 code
// C# program to find missing Number
// using xor
usingSystem;
classGFG {
// Function to find missing number
staticintgetMissingNo(int[] a, intn)
{
intx1 = a[0];
intx2 = 1;
/* For xor of all the elements
in array */
for(inti = 1; i < n; i++)
x1 = x1 ^ a[i];
/* For xor of all the elements
from 1 to n+1 */
for(inti = 2; i <= n + 1; i++)
x2 = x2 ^ i;
return(x1 ^ x2);
}
/* driver program to test above function */
publicstaticvoidMain()
{
int[] a = { 1, 2, 4, 5, 6 };
intmiss = getMissingNo(a, 5);
Console.Write(miss);
}
}
// This code is contributed by Sam007_
chevron_right
filter_none
PHP
filter_none
edit close
play_arrow
link brightness_4 code
<?php
// PHP program to find
// the Misiing Number
// getMissingNo takes array and
// size of array as arguments
functiongetMissingNo($a, $n)
{
// For xor of all the
// elements in array
$x1= $a[0];
// For xor of all the
// elements from 1 to n + 1
$x2= 1;
for($i= 1; $i< $n; $i++)
$x1= $x1^ $a[$i];
for($i= 2; $i<= $n+ 1; $i++)
$x2= $x2^ $i;
return($x1^ $x2);
}
// Driver Code
$a= array(1, 2, 4, 5, 6);
$miss= getMissingNo($a, 5);
echo($miss);
// This code is contributed by Ajit.
?>
chevron_right
filter_none
Output:
3
Complexity Analysis:
Time Complexity: O(n). Only one traversal of the array is needed.
Space Complexity: O(1). No extra space is needed.
Method 3: This method will work only in python. Approach: Take the sum of all elements in the array and subtract that from the sum of n+1 elements. For Eg: If my elements are li=[1,2,4,5] then take the sum of all elements in li and subtract that from the sum of len(li)+1 elements. The following code shows the demonstration.
C++
filter_none
edit close
play_arrow
link brightness_4 code
// C++ program to find
// the mising Number
#include<bits/stdc++.h>
usingnamespacestd;
// getMissingNo takes list
// as argument
intgetMissingNo(inta[] ,
intn)
{
intn_elements_sum = n * (n + 1) / 2 ;
intsum = 0;
for(inti = 0; i < n - 1; i++)
sum += a[i];
returnn_elements_sum-sum;
}
// Driver code
intmain()
{
inta[] = {1, 2, 4, 5, 6};
intn = sizeof(a) /
sizeof(a[0]) + 1;
intmiss = getMissingNo(a, n);
cout << (miss);
return0;
}
// This code is contributed by Chitranayal
chevron_right
filter_none
Java
filter_none
edit close
play_arrow
link brightness_4 code
// Java program to find
// the missing Number
classGFG{
// getMissingNo function for
// finding missing number
staticintgetMissingNo(inta[], intn)
{
intn_elements_sum = n * (n + 1) / 2;
intsum = 0;
for(inti = 0; i < n - 1; i++)
sum += a[i];
returnn_elements_sum - sum;
}
// Driver code
publicstaticvoidmain(String[] args)
{
inta[] = { 1, 2, 4, 5, 6};
intn = a.length + 1;
intmiss = getMissingNo(a, n);
System.out.print(miss);
}
}
// This code is contributed by mark_85
chevron_right
filter_none
Python3
filter_none
edit close
play_arrow
link brightness_4 code
# Python3 program to find
# the mising Number
# getMissingNo takes list as argument
defgetMissingNo(a, n):
n_elements_sum=n*(n+1)//2
returnn_elements_sum-sum(a)
# Driver program to test above function
if__name__=='__main__':
a =[1, 2, 4, 5, 6]
n =len(a)+1
miss =getMissingNo(a, n)
print(miss)
chevron_right
filter_none
Output:
3
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy