Find the Missing Number

Find the Missing Number
1. XOR all the array elements, let the result of XOR be a1.
2. XOR all numbers from 1 to n, let XOR be a2.
3. XOR of  a1 and a2 gives the missing number.

#include<stdio.h>

int MissingNo(int a[], int n)
{
    int i;
    int a1 = a[0];
    int a2 = 1;
   
    for (i = 1; i< n; i++)
        a1 = a1^a[i];
         
    for ( i = 2; i <= n+1; i++)
        a2 = a2^i;      
 
    return (a1^a2);
}

int main()
{
    int a[] = {1,2,4,5,6,7,8};
    int Number = MissingNo(a,7);
    printf("%d\n",Number);

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics