fork(1) download
  1. // c++ program for the above approach
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void solve(int arr[], int n){
  6. int x=0,y=0;
  7. int xor1=0;
  8. for(int i=0;i<n;i++){
  9. xor1=arr[i]^xor1;
  10. }
  11. for(int i=0;i<n;i++){
  12. xor1= xor1^i;
  13. }
  14. int set_bit_xor = xor1& ~(xor1-1);
  15.  
  16. for(int i=0;i<n;i++){
  17. if (arr[i] & set_bit_xor)
  18. { x ^= arr[i]; }
  19. else
  20. { y ^= arr[i]; }
  21.  
  22. // Decide whether (i+1) is in first set
  23. // or second
  24. if ((i+1) & set_bit_xor)
  25. { x ^= (i + 1); }
  26. else
  27. { y ^= (i + 1); }
  28. }
  29. cout << "Missing and Repeating (In any order) "
  30. << x << " " << y;
  31. }
  32.  
  33. int main() {
  34. int n;
  35. cin>>n;
  36. int arr[n];
  37. for(int i=0;i<n;i++)
  38. cin>>arr[i];
  39. solve(arr,n);
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5284KB
stdin
5
1 2 3 5 1
stdout
Missing and Repeating (In any order) 0 5