Advertisement
SajolLol

TwoDistinctNumberUsingBitwiseXORandOR

Oct 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e6;
  4. int arr[N];
  5.  
  6. bool hasBitSet(int n, int x) {
  7. int tem = n & (1<<x);
  8. return tem!=0;
  9. }
  10.  
  11. int main() {
  12. int n;
  13. cin >> n;
  14.  
  15. int all = 0;
  16. int ans[2] = {0,0};
  17.  
  18. for(int i=0; i<n; i++){
  19. cin >> arr[i];
  20. all ^= arr[i];
  21. }
  22.  
  23. //assert(all != 0);
  24. int k = 0;
  25. while( hasBitSet(all, k) == 0 ) k++;
  26.  
  27. //parition array into two sets: kth bit on v/s off
  28. for (int i=0; i<n; i++) {
  29. ans[hasBitSet(arr[i], k)] ^= arr[i];
  30. }
  31.  
  32. cout << ans[0] << " " << ans[1] << endl;
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement