fork download
  1. #include<stdio.h>
  2.  
  3. /* getMissingNo takes array and size of array as arguments*/
  4. int getMissingNo(int a[], int n)
  5. {
  6. int i;
  7. int x1 = a[0]; /* For xor of all the elements in array */
  8. int x2 = 1; /* For xor of all the elements from 1 to n+1 */
  9.  
  10. for (i = 1; i< n; i++)
  11. x1 = x1^a[i];
  12.  
  13. for ( i = 2; i <= n+1; i++)
  14. x2 = x2^i;
  15.  
  16. return (x1^x2);
  17. }
  18.  
  19. /*program to test above function */
  20. int main()
  21. {
  22. int a[] = {1, 2, 4, 5, 6};
  23. int miss = getMissingNo(a, 5);
  24. printf("%d", miss);
  25. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
3