# Find the missing number in a given integer array.

## In Phone Screen For Software Engineering Interview, You will be asked Question on Data Type Array.

Most of the time we use data structure String and Array to manage and fetch data. So It is very important for you to know to manipulate Array data with O(N) Time Complexity and O(1) Space Complexity!

Here is the Interview Question you may be asked during Screen Process.

### Question: Find the missing number in a given integer array of 1 to 100.

Details: 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, .., 100}

Output: 5

Explanation: The missing number from 1 to 100 is 5

Input: arr[] = {1, 2, 3, 5, .., 100}

Output: 4

Explanation: The missing number from 1 to 100 is 4

To Solve this Problem we will find total for all numbers. For that all you need to use correct Math equation.

Total of Numbers from Array = (n + 1) * (n+2) /2;

Where n = array length.

Example1:

Given Array = [1, 2 , 4, 5] = Array Length = 4 => Total = (5) * (6) /2 = 15

Given Array = [1, 2 ,3, 5, 6] = Array Length = 5 => Total = (6) * (7) /2 = 21

Now we will scan through array and subtract current integer from total.

For Example, You have given Array = [1, 2 , 4, 5] .

Find out the Array Length = Which is 4

Next Find out the Total Which is = (4 + 1 = 5) * ( 4 + 2 = 6) /2 = 15.

Now scan the Array and we will remove 1 => 15 – 1 = 14. With second scan we will remove 2 => 14 – 2 = 12 Third Scan we will remove 4 => 12 – 4 = 8 Fourth Scan we will remove 5 => 8 – 5 = 3 So we figured out that missing number is 3!

Let’s Add All together in Programing Function as working Solution.

``````const missingNumber = (arr) => {
const n = arr.length;
let total = (n + 1) * (n + 2) /2;
for (let i = 0; i < n; i++) {
total -= arr[i];
}