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.

Program:

#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];
    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();
}

Time Complexity: O(n)



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 elemets in arary */
    int x2 = 1; /* For xor of all the elemets 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)

In method 1, if the sum of the numbers goes beyond maximum allowed integer, then there can be integer overflow and we may not get correct answer. Method 2 has no such problems.





  • Sourabh Upadhyay

    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.
    instead of total=n*(n+1)/2:
    add total=total+i+1 in the loop already used.

  • 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;

      }

  • 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.

    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.
    please help…

     
    /* 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

    How about this?

     
    #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

  • http://wgpshashank.co.cc wgpshashank

    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

  • http://wemeanourthoughts.blogspot.com/ Venki

    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

        My bad! :)

  • GeeksforGeeks

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

  • TJ

    @rv can you please explain your solution?

  • 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