jasonpogi1669

Bitwise XOR to find an odd number using C++

Nov 4th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> a(n);
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     // using XOR property: a ^ a = 0 and 0 ^ a = a
  13.     int res = 0;
  14.     for (int i = 0; i < n; i++) {
  15.         res ^= a[i];
  16.     }
  17.     cout << res << '\n';
  18.     return 0;
  19. }
  20.  
Add Comment
Please, Sign In to add comment