#include <stdio.h>
#include <stdlib.h>
int missingNo(int *a, int n)
{
int x = 0, y = 0;
for (int i = 0; i < n - 1; i++)
{
// xoring all elements
x ^= a[i];
}
for (int i = 1; i <= n; i++)
{
// xoring 1 to n
y ^= i;
}
// xoring x & y outputs missing number
return x ^ y;
}
int main()
{
int n, x, count = 0;
printf("enter your size : ");
scanf("%d", &n);
printf("enter elements 1 to n : ");
// dynamic array created for n-1 elements
int *a = (int *)(malloc(sizeof(int) * (n - 1)));
for (int i = 0; i < n - 1; i++)
{
scanf("%d", &a[i]);
}
// function to check duplicate exists or not
printf("\nthe missing number is %d\n", missingNo(a, n));
return 0;
}
output
enter your size : 5
enter elements 1 to n : 1 2 3 5 6
the missing number is 4
0 Comments