# 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 list. One of the integers is missing in the list. Write an efficient code to find the missing integer.

```Example:
I/P    [1, 2, 4, ,6, 3, 7, 8]
O/P    5
```

METHOD 1(Use sum formula)

Algorithm:

```1. Get the sum of numbers
total = n*(n+1)/2
2  Subtract all the numbers from sum and
you will get the missing number.
```
```#include<stdio.h>

/* 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];
}

/*program to test above function */
int main()
{
int a[] = {1,2,4,5,6};
int miss = getMissingNo(a,5);
printf("%d", miss);
getchar();
}
```

Time Complexity: O(n)

There can be overflow if n is large. In order to avoid Integer Overflow, we can pick one number from known numbers and subtract one number from given numbers. This way we wont have Integer Overflow ever. Thanks to Sahil Rally for suggesting this improvement.

METHOD 2(Use XOR)

```  1) XOR all the array elements, let the result of XOR be X1.
2) XOR all numbers from 1 to n, let XOR be X2.
3) XOR of X1 and X2 gives the missing number.
```
```#include<stdio.h>

/* getMissingNo takes array and size of array as arguments*/
int getMissingNo(int a[], int n)
{
int i;
int x1 = a[0]; /* 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();
}
```

Time Complexity: O(n)

# Company Wise Coding Practice    Topic Wise Coding Practice

• Kaushal

can anyone explain logic of second method

• rishabh joshi

here while taking sum method it may happen that we might exceed integer limit,if we are not allowed to use long long then?
suppose one number is 10^9 and other is 10^7 will the XOR of this will be out of bound of integer limit??

• Can also be done using HashMap. Maintain a HashMap for smaller array and decrement the count on the HashMap for every element in bigger array.
Time Complexity : O(n)
Space Complexity : O(n)

If you must know, Xor of n numbers f(n)=(1^2^3….n) can be calculated in O(1)
if(n%4 ==0) f(n)=n
if(n%4==1) f(n)=1
if(n%4==2) f(n)=n+1
if(n%4==3) f(n)=0
Hence method 2 can be modified to do this

• rishabh joshi

will you please explain your modificatio and how you will use it π

• Deepesh Panjabi
• Anuj Garg

w = ((len(a) + 1) * ( len(a) + 2 ) )//2

what is double slash.

• Mrigank

This method also works:-

1) Suppose we have been given an array,total elements and the range 1-n of the numbers and we have to find the missing number,then..

2) we begin a loop till n from i=0 and check if :-

a) arr[i] -1 is greater or equal to n, then i++

b) if arr[i]<0 then take abs(arr[i]) and negate the arr[abs(arr[i])-1]

c) else negate the arr[arr[i]-1]

We have taken arr[i]-1 because n(the number of elements of array) will be 1 less than the n(of range) , eg 1-10 is the range and array has 9 elements.

3) scan the array again and return index+1 of that element which remains positive, rest all will be negative, that will be the answer.

Here is the code demonstrating that, Please do let me know if you find exceptions!!

#include

#include

int missing(int arr[],int n)

{

int i=0;

while(i= n)

i++;

else if(arr[i] < 0)

{

arr[abs(arr[i])-1]=-(arr[abs(arr[i])-1]);

i++;

}

else

{

arr[arr[i]-1]=-arr[arr[i]-1];

i++;

}

}

for(i=0;i0)

return i+1;

}

}

int main()

{

int arr[]={3,10,1,4,9,5,2,7,8};

printf(“missing number is %dn”,missing(arr,9));

return 0;

}

• Mrigank

Sorry the code didn’t get pasted right, here is the link for your reference

• Avish

Actually this will be correct, minor bug in your code. It will throw error if we have all the elements from 1-n and the number is also n.

• GuestD

It’s also wrong. Try for this:-

{3,10,1,4,9,5,2,7,8}

For correction, remove “return n + 1” from else and put if outside the for loop.! π

• Farid Khan

#include

int getMissingNumber(int[],int);
int main(void) {
int a[]={8,9,2,3,4,5,6,10,7};
printf(“%d”,getMissingNumber(a,9));
return 0;
}

int getMissingNumber(int a[],int arraySize){
int max=0;
int total=0;
int i=0;
for(i=0;imax){max=a[i];}
total=total+a[i];
}
return (max*(max+1)/2)-total;
}

Time Complexity : O(n)

• Anubhav

is it possible to do it using a hashmap..???
Will the worst case time be worse than what it is with the 2nd method or will it be the same..???

This can also be solved in O(n) by marking arr[arr[i]] = -arr[arr[i]] and finding the first positive number if no number is found then return n.

• mb1994

overflow can be avoided in the first algorithm.

• wrestler

Finally you are also storing total, so u r also not avoiding it

• gandhi_rahul

What if two numbers are missing? and can we generalize it for ‘k’ elements?

• Meenal

Why do we need n+1 in second loop, and not n i.e

for ( i = 2; i <= n+1; i++)
x2 = x2^i;

Can anyone explain?

• Sumit Khanna

Another solution could be taking index=Arr[i] for i =0->n-1 … and negating the value at Arr[index] … after we are done with above procedure for all elements,,run a loop to check which element Arr[i] is positive,,return i .

• Amit Baghel

Visited π

• Kedar

class example

{

public example()

{

int a[]={1,4,3,2,6};

int i,j,temp;

for(i=0;i<a.length;i++)

for(j=0;j<a.length;j++)

if(a[i]<a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

for(int k=0;k<a.length;k++)

{

if(a[k]!=k+1)

{

System.out.print("The missing number is "+(k+1));

break;

}

}

}

}

class program

{

public static void main(String[] args)

{

example ex = new example();

}

}

• vishal11

how can we find the missing nos if they are more than 1
Ex-{1,2,3,5,7}
output-4,6

• guest

Will this help ?

void find_occ(int a[], int n)
{
int i = 0, t = 0;
while(i n)) {
i++;
}
else {
t = a[a[i] – 1];
a[a[i] – 1] = a[i];
a[i] = t;
}
}

for(i = 0; i i + 1)
cout << i + 1 << " missing" << endl;

}

• RK

• sandhanapandianrr

#include
#include

int main()
{

int a[] = {1, 2, 4, 5,6};
int i;
for(i=0;i<4;i++)
{
if(a[i+1]-a[i]!=1)
{
printf("%d", a[i]+1);
}
}
return 0;
}

• NK

Only if the input is sorted, XOR approach works on un-ordered array too.

• Kaustav Chatterjee

We can sort the array first and check each number…which doesn’t match return that…

#include
void quickSort(int[],int,int);
int partition(int[],int,int);
void exchange(int*,int*);
int GetMissingNumber(int[],int);
int main()
{
int i,n,b,a[100];
printf(“Enter the no of elements of the array\n”);
scanf(“%d”,&n);
printf(“Enter the array\n”);
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
}
quickSort(a,0,n-1);
b = GetMissingNumber(a,n);
printf("Missing number is : %d",b);
return 0;
}
int GetMissingNumber(int a[],int n)
{
int i;
for(i = 0;i<n;i++)
{
if(a[i]!=i+1)
{
return(i+1);
}
}
}
/*Below fuctions are only for Sorting*/
void quickSort(int a[], int si, int ei)
{
int pi; /* Partitioning index */
if(si < ei)
{
pi = partition(a, si, ei);
quickSort(a, si, pi – 1);
quickSort(a, pi + 1, ei);
}
}
int partition(int a[], int si, int ei)
{
int x = a[ei];
int i = (si – 1);
int j;

for (j = si; j <= ei – 1; j++)
{
if(a[j] <= x)
{
i++;
exchange(&a[i], &a[j]);
}
}
exchange (&a[i + 1], &a[ei]);
return (i + 1);
}
void exchange(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

• Abhay

Hashing can also be applied. Let array length be X. For each a[i], increase a[(a[i]-1)%a.length] by a fixed quantity, say C. Traverse the array again , to find the number which is less than C (Take C larger than a.length, I have taken N, which is same as n taken in queston), and return it’s index. If no such quantity exists then it is N, which is then returned

``` ```
class Solver1
{
public int SolverUtil(int a[])
{
int N=a.length+1;
for(int i=0;i<a.length;i++)
{
a[(a[i]-1)%(a.length)]+=N;
}
int M=-1;
for(int j=0;j<a.length;j++)
{
if(a[j]<N)
return j+1;
}
return N;
}
}
public class FindMissing
{

public static void main(String args[])
{
Solver1 ob1=new Solver1();
int a[]={1,2,4,6,3,7,8};
System.out.println(ob1.SolverUtil(a));
}
}
``` ```
• Abhay

The line “Take C larger than a.length, I have taken N, which is same as n taken in queston” is incorrect, C should be chosen only a.length.

Corrected code is:

``` ```
class Solver1
{
public int SolverUtil(int a[])
{
int N=a.length+1;
for(int i=0;i<a.length;i++)
{
a[(a[i]-1)%(a.length)]+=a.length;
}
for(int j=0;j<a.length;j++)
{

if(a[j]<a.length)
return j+1;
}
return N;
}
}
public class Main
{

public static void main(String args[])
{
Solver1 ob1=new Solver1();
int a[]={1,2,5,4,6,3,7,8,9,12,10};
System.out.println(ob1.SolverUtil(a));
}
}
``` ```
• Nagaraju

Scan from left to right and make value at index a[i] to negative. Next pass is to scan from index 1 to array size find non negative number and return index of the number. If we did not find any such index return n.

[sourcecode language="JAVA"]
public static double getMissingValueMethod2(int[]a, int n){
for(int i =0; i < n-1; i++)
{
int t = Math.abs(a[i]);
if(t < n-1)
{
a[t] = -a[t];
}
}

for(int k =1; k < n-1; k++)
{
if(a[k] > 0) return k;

}
return n;
}

• Nagaraju

public static double getMissingValueMethod2(int[]a, int n){
for(int i =0; i < n-1; i++)
{
int t = Math.abs(a[i]);
if(t < n-1)
{
a[t] = -a[t];
}
}

for(int k =1; k 0) return k;

}
return n;
}

• Chinmaya

As the range of numbers is given,create a hash table. For every element in the array,change the corresponding value in the hash table to 1. Now look for 0 in the hash table and thus its index as the solution.

Its complexity will be O(n).

• Shivam Maharshi

Can we also do this with Binary Search? Time complexity will only be O(log(n)).

• GeeksforGeeks

Binary Search can not be applied as the given array is not sorted.

• Shivam Maharshi
``` ```
Yeah, didn't notice that.
``` ```
• anonymous

Even if it WAS sorted. What would you be searching for?

• Aman

I am not getting how the logic of xor is working. Whats the mathematical or analytical proof for this 2nd method.

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• Aman

i got it..
take xor to be (.) operator , its logic is..
if we take two sequences (a,b,c,d,e)& (a,b,c,d)
(a.b.c.d.e).(a.b.c.d)
=(a.a).(b.b).(c.c).(d.d).e
=0.0.0.0.e
=0.e
=e

• rohith

Same problem…. If you have found the answer for the logic behind using XOR, then please reply with the logic.
Thank you.

• rohith

Oh u have replied π ..
Thank you..

• kapil kumar chawala

Thanks, for the XOR logic, I got it…

• Arpit
``` ```
public static void findMissing(Integer[] arr){
int i=0, x1=arr[0],x2=1^2;
for (i=1;i<arr.length;i++){
x1 = x1 ^ arr[i];

x2 = x2^(i+2);
}

System.out.println("Missing (No Duplicates)- "+ (x1^x2));

}

public static void main(String[] args) {
// TODO Auto-generated method stub
try{

Integer[] arr = {1,2,4,3,6,7};
findDuplicate(arr);
findMissing(arr);
}
catch( Exception e){
System.err.println(e.getMessage());
}
}
``` ```
• cxin

i like your code. the original post return wrong answer when missing 1

• pr6989
``` ```
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a[]={1,2,4,6,3,7,8};
int size=sizeof(a)/sizeof(a[0]);
int *arr=(int*)malloc(sizeof(int)*(size+1));
int i;

for(i=0;i<(size+1);i++)
arr[i]=0;

for(i=0;i<size;i++)
arr[a[i]-1]++;

for(i=0;i<(size+1);i++)
{
if(arr[i]==0)
{
printf("Missing number is = %d\n",i+1);
break;
}
}
free(arr);
return 0;
}
``` ```
• pr6989

The above method should be O(n)

1.I’m creating a dynamic array “arr” of size= 1+size of given array and initializing it to zero.
2. Scan the given array and increment the count of the element in array “arr” at index a[i]-1.
3. Scan the array “arr”.Whichever index has value=0 that index+1 gives the missing number.

• Yogesh Batra

But you are using extra space O(n), it’ll be an overhead. Why should we use extra space if we can solve the same thing without using extra space?

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• pr6989

``` ```
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a[]={1,2,4,6,3,7,8};
int size=sizeof(a)/sizeof(a[0]);
int *count=(int*)malloc(sizeof(int)*(size+1));
int i;

for(i=0;i<(size+1);i++)
count[i]=0;

for(i=0;i<size;i++)
count[a[i]-1]++;

for(i=0;i<(size+1);i++)
{
if(count[i]==0)
{
printf("Missing number is = %d\n",i+1);
break;
}
}
free(count);
return 0;
}
``` ```

1. I’m creating an array named “count” of size = 1 + size of the given array and initializing it to zero.
2. Scan the array and increment the count of the element as you go along.
3. Scan the “count” array to see which index has value=0. The missing number will be = (index+1).

Should be O(n)

• Shyam

@geeksforgeeks

these methods do not work if the missing element in the list is n… (i.e) if numbers from 1 to n-1 are given and n is missing then incorrect answer of 0 will be obtained unless we give the upper limit of n as an argument to the function

• kartik

@shyam: Take a closer look at the programs. They all take size of array as parameter. In the first method, ff the size is n, then sum of n+1 elements is considered. In second method, xor of n+1 numbers is calculated to make sure that the last missing case is handled. You could try running the programs for some sample inputs.

• Meenal

Why do we need n+1 in second case. Can you please explain logic?

• neilljohnson

>>> a=range(1,101,1)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> import random
>>> random.shuffle(a)
>>> a
[24, 3, 2, 60, 90, 95, 8, 40, 49, 53, 58, 97, 66, 7, 82, 6, 81, 22, 72, 12, 91, 76, 1, 84, 54, 20, 21, 38, 59, 78, 27, 36, 5, 15, 26, 86, 67, 98, 99, 65, 79, 94, 30, 80, 17, 44, 43, 29, 33, 73, 35, 61, 19, 32, 47, 62, 93, 57, 92, 4, 88, 23, 16, 83, 28, 71, 70, 89, 39, 42, 9, 63, 52, 45, 56, 96, 34, 64, 25, 31, 74, 13, 10, 50, 37, 14, 77, 11, 18, 68, 69, 85, 55, 48, 87, 41, 75, 51, 46, 100]
>>> a.pop()
100
>>>
>>> a
[24, 3, 2, 60, 90, 95, 8, 40, 49, 53, 58, 97, 66, 7, 82, 6, 81, 22, 72, 12, 91, 76, 1, 84, 54, 20, 21, 38, 59, 78, 27, 36, 5, 15, 26, 86, 67, 98, 99, 65, 79, 94, 30, 80, 17, 44, 43, 29, 33, 73, 35, 61, 19, 32, 47, 62, 93, 57, 92, 4, 88, 23, 16, 83, 28, 71, 70, 89, 39, 42, 9, 63, 52, 45, 56, 96, 34, 64, 25, 31, 74, 13, 10, 50, 37, 14, 77, 11, 18, 68, 69, 85, 55, 48, 87, 41, 75, 51, 46]
>>> sum(a)
4950
>>> sum(range(101))
5050
>>> missingnumber=sum(range(101))-sum(a)
>>> missingnumber
100

• we are doing xor two time because its becomes duplicted aray & only one element occurs odd times..dan final xor=x1^x2 will give us..missing number ..isn’t it

• In method 2, why do we need two loops? We need one extra XOR operation with n+1, we can get rid of loop.

Further we can optimize the XOR technique. Assuming N as power of 2, then 1 XOR 2 XOR 3 XOR 4 XOR …. XOR N = N. It can be done in O(1) time.

• ravikant

can somebody please how the XOR logic works ????

• Suesh PV

Given an array of n-2 elements, how do you find the two missing elements.

• kartik

The trick used @http://geeksforgeeks.org/?p=2457 can also be used here. You can XOR the whole array and XOR the result with all numbers from 1 to n.

• Jithan

Solution can be calculated in O(logn). If the a[n/2] = n/2, then the missing number is after n/2, else it is before n/2. Repeat the process to find the number. Similar to binary search.

• kartik

@Jitin: The solution that your are suggesting works for sorted array only.

In the above given solutions, array is not assumed to be sorted.

• Jithan

• GeeksforGeeks

TJ & rv_10987: We have added a new method (please see method 2) that doesn’t suffer from integer overflow problem.

• TJ

• rv_10987

to handle the overflow condition we can do this thing-
a[a[i]]*=-1;
Now traverse the array,print the index value of the only positive element..!!

• TJ

Although it is a short and sweet way of solving this problem, it creates a potential bug because the sum may overflow beyond the integer boundaries – thus leading to incorrect results.
This could be solved by continuously swapping elements to bring them into their correct positions, i.e. A[i] = i;
This is O(n) but destroys the originality of the list

• geeksforgeeks

@Deepak: Thanks very much. We have corrected the same.

• Deepak

1. Get the sum of numbers
total = n*(n-1)/2

Its n*(n+1)/2