# Find the one missing number in range

Given an array of size n. It is also given that range of numbers is from smallestNumber to smallestNumber + n where smallestNumber is the smallest number in array. The array contains number in this range but one number is missing so the task is to find this missing number.

Examples:

```Input  :  arr[] = {13, 12, 11, 15}
Output :  14

Input  :  arr[] = {33, 36, 35, 34};
Output : 37
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The problem is very close to find missing number.

There are many approaches to solve this problem.
A simple approach is to first find minimum, then one by one search all elements. Time complexity of this approach is O(n*n)

A better solution is to sort the array. Then traverse the array and find the first element which is not present. Time complexity of this approach is O(n Log n)

The best solution is to first XOR all the numbers. Then XOR this result to all numbers from smallest number to n+smallestNumber. The XOR is our result.

Example:-

```arr[n] = {13, 12, 11, 15}
smallestNumber = 11

first find the xor of this array
13^12^11^15 = 5

Then find the XOR first number to first number + n
11^12^13^14^15 = 11;

Then xor these two number's
5^11 = 14 // this is the missing number
```

## C++

 `// CPP program to find missing ` `// number in a range. ` `#include ` `using` `namespace` `std; ` ` `  `// Find the missing number ` `// in a range ` `int` `missingNum(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `minvalue = *min_element(arr, arr+n); ` ` `  `    ``// here we xor of all the number ` `    ``int` `xornum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``xornum ^= (minvalue) ^ arr[i]; ` `        ``minvalue++; ` `    ``} ` ` `  `    ``// xor last number ` `    ``return` `xornum ^ minvalue; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 13, 12, 11, 15 }; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` `    ``cout << missingNum(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find ` `// missing number in a range. ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` `     `  `// Find the missing number in a range ` `static` `int` `missingNum(``int` `arr[], ``int` `n) ` `{ ` `    ``List list = ``new` `ArrayList<>(arr.length); ` `    ``for` `(``int` `i :arr) ` `    ``{ ` `        ``list.add(Integer.valueOf(i)); ` `    ``} ` `    ``int` `minvalue = Collections.min(list);; ` `  `  `    ``// here we xor of all the number ` `    ``int` `xornum = ``0``; ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `        ``xornum ^= (minvalue) ^ arr[i]; ` `        ``minvalue++; ` `    ``} ` `  `  `    ``// xor last number ` `    ``return` `xornum ^ minvalue; ` `} ` ` `  `public` `static` `void` `main (String[] args) { ` `     ``int` `arr[] = { ``13``, ``12``, ``11``, ``15` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println(missingNum(arr, n));    ` ` `  `    ``} ` `} ` ` `  `//This code is contributed by Gitanjali. `

## Python3

 `# python3 program to check ` `# missingnumber in a range ` ` `  `# Find the missing number  ` `# in a range ` `def` `missingNum( arr,  n): ` ` `  `    ``minvalue ``=` `min``(arr) ` `  `  `    ``# here we xor of all the number ` `    ``xornum ``=` `0` `    ``for`  `i ``in` `range` `(``0``,n): ` `        ``xornum ^``=` `(minvalue) ^ arr[i] ` `        ``minvalue ``=` `minvalue``+``1` `     `  `  `  `    ``# xor last number ` `    ``return` `xornum ^ minvalue ` `     `  `# Driver method ` `arr ``=` `[ ``13``, ``12``, ``11``, ``15` `] ` `n ``=` `len``(arr) ` `print` `(missingNum(arr, n)) ` ` `  `# This code is contributed by Gitanjali. `

## C#

 `// C# program to find ` `// missing number in a range. ` `using` `System; ` `using` `System.Collections.Generic; ` `using` `System.Linq; ` ` `  `class` `GFG  ` `{ ` `     `  `// Find the missing number in a range ` `static` `int` `missingNum(``int` `[]arr, ``int` `n) ` `{ ` `    ``List<``int``> list = ``new` `List<``int``>(arr.Length); ` `    ``foreach` `(``int` `i ``in` `arr) ` `    ``{ ` `        ``list.Add(i); ` `    ``} ` `    ``int` `minvalue = list.Min(); ` ` `  `    ``// here we xor of all the number ` `    ``int` `xornum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``xornum ^= (minvalue) ^ arr[i]; ` `        ``minvalue++; ` `    ``} ` ` `  `    ``// xor last number ` `    ``return` `xornum ^ minvalue; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main (String[] args) ` `{ ` `    ``int` `[]arr = { 13, 12, 11, 15 }; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(missingNum(arr, n));  ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## PHP

 ` `

Output:

```14
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Sach_Code, Rajput-Ji

Article Tags :
Practice Tags :

1

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