fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n; //number of elements
  6.  
  7. cout << "Enter the number of elements : ";
  8. cin >> n;
  9.  
  10. //Declaring array
  11. int arr[n-1];
  12.  
  13. //Taking input
  14. for(int i = 0; i<n-1; i++)
  15. cin >> arr[i];
  16.  
  17. //printing array
  18. cout << "\nElements are :";
  19. for(int i = 0; i<n-1; i++)
  20. cout << " " << arr[i];
  21.  
  22. //Declaring elements to take XOR
  23. int x1 = arr[0]; // first element of array
  24. int x2 = 1; //first element in natural number series i.e 1,2,3,4...
  25.  
  26. //taking XOR of all elements of the given array
  27. for(int i = 1; i<n-1; i++)
  28. x1 ^= arr[i];
  29.  
  30. //taking XOR of all the natural numbers till 'n'
  31. for(int i = 2; i<arr[n-1]; i++)
  32. x2 ^= i;
  33.  
  34. //Finally printing the output by taking XOR of x1 and x2
  35. //because same numbers will be removed since (A^A) = 0 also (A^0) = A
  36. cout << "\nMissing number : " << (x1^x2) << endl;
  37. return 0;
  38. }
Success #stdin #stdout 0.66s 15232KB
stdin
9
1 2 3 4 5 7 8 9


stdout
Enter the number of elements : 
Elements are : 1 2 3 4 5 7 8 9
Missing number : 7