# Find the Missing Number

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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Method 1: This method uses the technique of 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:
1. Calculate the sum of first n natural numbers as sumtotal= n*(n+1)/2
2. create a variable sum to store the sum of array elements.
3. Traverse the array from start to end.
4. Update the value of sum as sum = sum + array[i]
5. print the missing number as sumtotal – sum
• Implementation:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Function to get the missing number ` `int` `getMissingNo(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``int` `total = (n + 1) * (n + 2) / 2; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``total -= a[i]; ` `    ``return` `total; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 4, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `miss = getMissingNo(arr, n); ` `    ``cout << miss; ` `} `

## C

 `#include ` ` `  `/* getMissingNo takes array and size of array as arguments*/` `int` `getMissingNo(``int` `a[], ``int` `n) ` `{ ` `    ``int` `i, total; ` `    ``total = (n + 1) * (n + 2) / 2; ` `    ``for` `(i = 0; i < n; i++) ` `        ``total -= a[i]; ` `    ``return` `total; ` `} ` ` `  `/*program to test above function */` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 4, 5, 6 }; ` `    ``int` `miss = getMissingNo(a, 5); ` `    ``printf``(``"%d"``, miss); ` `    ``getchar``(); ` `} `

## Java

 `// Java program to find missing Number ` ` `  `class` `Main { ` `    ``// Function to ind missing number ` `    ``static` `int` `getMissingNo(``int` `a[], ``int` `n) ` `    ``{ ` `        ``int` `i, total; ` `        ``total = (n + ``1``) * (n + ``2``) / ``2``; ` `        ``for` `(i = ``0``; i < n; i++) ` `            ``total -= a[i]; ` `        ``return` `total; ` `    ``} ` ` `  `    ``/* program to test above function */` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `a[] = { ``1``, ``2``, ``4``, ``5``, ``6` `}; ` `        ``int` `miss = getMissingNo(a, ``5``); ` `        ``System.out.println(miss); ` `    ``} ` `} `

## Python

 `# getMissingNo takes list as argument ` `def` `getMissingNo(A): ` `    ``n ``=` `len``(A) ` `    ``total ``=` `(n ``+` `1``)``*``(n ``+` `2``)``/``2` `    ``sum_of_A ``=` `sum``(A) ` `    ``return` `total ``-` `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 `

## C#

 `// C# program to find missing Number ` `using` `System; ` ` `  `class` `GFG { ` `    ``// Function to ind missing number ` `    ``static` `int` `getMissingNo(``int``[] a, ``int` `n) ` `    ``{ ` `        ``int` `total = (n + 1) * (n + 2) / 2; ` ` `  `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``total -= a[i]; ` ` `  `        ``return` `total; ` `    ``} ` ` `  `    ``/* program to test above function */` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] a = { 1, 2, 4, 5, 6 }; ` `        ``int` `miss = getMissingNo(a, 5); ` `        ``Console.Write(miss); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007_ `

## PHP

 ` `

Output :

`3`
• Compelxity Analysis:
• Time Complexity: O(n).
Only one traversal of 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:
1. create a variable sum = 1 to which will store the missing number and a counter c = 2.
2. Traverse the array from start to end.
3. Update the value of sum as sum = sum – array[i] + c and update c as c++.
4. print the missing number as sum.
• Implementation:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// a represents the array ` `// n : Number of elements in array a ` `int` `getMissingNo(``int` `a[], ``int` `n)  ` `{  ` `    ``int` `i, total=1;  ` `     `  `    ``for` `( i = 2; i<= (n+1); i++) ` `    ``{ ` `        ``total+=i; ` `        ``total -= a[i-2]; ` `    ``} ` `    ``return` `total;  ` `}  ` ` `  `//Driver Program ` `int` `main() { ` `    ``int` `arr[] = {1, 2, 3, 5}; ` `    ``cout<

## Java

 `// Java implementation  ` `class` `GFG ` `{ ` ` `  `    ``// a represents the array ` `    ``// n : Number of elements in array a ` `    ``static` `int` `getMissingNo(``int` `a[], ``int` `n)  ` `    ``{ ` `        ``int` `total = ``1``; ` `        ``for` `(``int` `i = ``2``; i <= (n + ``1``); i++) ` `        ``{ ` `            ``total += i; ` `            ``total -= a[i - ``2``]; ` `        ``} ` `        ``return` `total; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``1``, ``2``, ``3``, ``5` `}; ` `        ``System.out.println(getMissingNo(arr, arr.length)); ` `    ``} ` `} ` ` `  `// This post is contributed  ` `// by Vivek Kumar Singh `

## Python3

 `# a represents the array ` `# n : Number of elements in array a ` `def` `getMissingNo(a, n):  ` `    ``i, total ``=` `0``, ``1` ` `  `    ``for` `i ``in` `range``(``2``, n ``+` `2``): ` `        ``total ``+``=` `i ` `        ``total ``-``=` `a[i ``-` `2``] ` `    ``return` `total ` ` `  `# Driver Code ` `arr ``=` `[``1``, ``2``, ``3``, ``5``] ` `print``(getMissingNo(arr, ``len``(arr))) ` ` `  `# This code is contributed by Mohit kumar `

## C#

 `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// a represents the array ` `// n : Number of elements in array a ` `static` `int` `getMissingNo(``int``[] a, ``int` `n)  ` `{  ` `    ``int` `i, total = 1;  ` `     `  `    ``for` `( i = 2; i <= (n + 1); i++) ` `    ``{ ` `        ``total += i; ` `        ``total -= a[i - 2]; ` `    ``} ` `    ``return` `total;  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main()  ` `{ ` `    ``int``[] arr = {1, 2, 3, 5}; ` `    ``Console.Write(getMissingNo(arr, (arr.Length))); ` ` `  `    ``// Console.Write(getMissingNo(arr, 4)); ` `} ` `} ` `// This code is contributed by SoumikMondal `

• Compelxity Analysis:
• Time Complexity: O(n).
Only one traversal of 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

Using this property, the missing element can be found. Calculate XOR of all the natural number from 1 to n and store it as a. Now calculate XOR of all the elements of the array and store it as b. The missing number will be a ^ b.
^ is XOR operator.

• Algorithm:
1. Create two variables a = 0 and b = 0
2. Run a loop from 1 to n with i as counter.
3. For every index update a as a = a ^ i
4. Now traverse the array from start to end.
5. For every index update b as b = b ^ array[i]
6. Print the missing number as a ^ b.
• Implementation:

## C++

 `#include ` `using` `namespace` `std; ` ` `  `// Function to get the missing number ` `int` `getMissingNo(``int` `a[], ``int` `n) ` `{ ` `    ``// For xor of all the elements in array ` `    ``int` `x1 = a; ` ` `  `    ``// For xor of all the elements from 1 to n+1 ` `    ``int` `x2 = 1; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``x1 = x1 ^ a[i]; ` ` `  `    ``for` `(``int` `i = 2; i <= n + 1; i++) ` `        ``x2 = x2 ^ i; ` ` `  `    ``return` `(x1 ^ x2); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 4, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `miss = getMissingNo(arr, n); ` `    ``cout << miss; ` `} `

## C

 `#include ` ` `  `/* getMissingNo takes array and size of array as arguments*/` `int` `getMissingNo(``int` `a[], ``int` `n) ` `{ ` `    ``int` `i; ` `    ``int` `x1 = a; ``/* For xor of all the elements in array */` `    ``int` `x2 = 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 */` `int` `main() ` `{ ` `    ``int` `a[] = { 1, 2, 4, 5, 6 }; ` `    ``int` `miss = getMissingNo(a, 5); ` `    ``printf``(``"%d"``, miss); ` `    ``getchar``(); ` `} `

## Java

 `// Java program to find missing Number ` `// using xor ` `class` `Main { ` `    ``// Function to find missing number ` `    ``static` `int` `getMissingNo(``int` `a[], ``int` `n) ` `    ``{ ` `        ``int` `x1 = a[``0``]; ` `        ``int` `x2 = ``1``; ` ` `  `        ``/* For xor of all the elements  ` `           ``in array */` `        ``for` `(``int` `i = ``1``; i < n; i++) ` `            ``x1 = x1 ^ a[i]; ` ` `  `        ``/* For xor of all the elements  ` `           ``from 1 to n+1 */` `        ``for` `(``int` `i = ``2``; i <= n + ``1``; i++) ` `            ``x2 = x2 ^ i; ` ` `  `        ``return` `(x1 ^ x2); ` `    ``} ` ` `  `    ``/* program to test above function */` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `a[] = { ``1``, ``2``, ``4``, ``5``, ``6` `}; ` `        ``int` `miss = getMissingNo(a, ``5``); ` `        ``System.out.println(miss); ` `    ``} ` `} `

## Python3

 `# Python3 program to find ` `# the mising Number ` `# getMissingNo takes list as argument  ` ` `  `def` `getMissingNo(a, n): ` `    ``x1 ``=` `a[``0``] ` `    ``x2 ``=` `1` `     `  `    ``for` `i ``in` `range``(``1``, n): ` `        ``x1 ``=` `x1 ^ a[i] ` `         `  `    ``for` `i ``in` `range``(``2``, n ``+` `2``): ` `        ``x2 ``=` `x2 ^ i ` `     `  `    ``return` `x1 ^ 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  `

## C#

 `// C# program to find missing Number ` `// using xor ` `using` `System; ` ` `  `class` `GFG { ` `    ``// Function to find missing number ` `    ``static` `int` `getMissingNo(``int``[] a, ``int` `n) ` `    ``{ ` `        ``int` `x1 = a; ` `        ``int` `x2 = 1; ` ` `  `        ``/* For xor of all the elements  ` `        ``in array */` `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``x1 = x1 ^ a[i]; ` ` `  `        ``/* For xor of all the elements  ` `        ``from 1 to n+1 */` `        ``for` `(``int` `i = 2; i <= n + 1; i++) ` `            ``x2 = x2 ^ i; ` ` `  `        ``return` `(x1 ^ x2); ` `    ``} ` ` `  `    ``/* driver program to test above function */` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] a = { 1, 2, 4, 5, 6 }; ` `        ``int` `miss = getMissingNo(a, 5); ` `        ``Console.Write(miss); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007_ `

## PHP

 ` `

Output:

`3`
• Complexity Analysis:
• Time Complexity: O(n).
Only one traversal of array is needed.
• Space Complexity: O(1).
No extra space is needed.

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.

My Personal Notes arrow_drop_up

Article Tags :

111

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.