fork download
  1. //This program finds the maximum XOR of two numbers which are shuffled in its binary form
  2. //e,g if numbers are 5 and 4 with size 3 in binary form
  3. //these will be 101 and 100 XOR is 001 but max can be 111 by shuffling 1st and second as 011 and 100
  4.  
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<string>
  8. #include<cmath>
  9. #include<bitset>
  10.  
  11. using namespace std;
  12.  
  13.  
  14.  
  15.  
  16. long long dec2bin(int dec) {
  17. long long rem,i=1,sum=0;
  18. do
  19. {
  20. rem=dec%2;
  21. sum=sum + (i*rem);
  22. dec=dec/2;
  23. i=i*10;
  24. }while(dec>0);
  25. return sum;
  26. }
  27.  
  28.  
  29. int main(){
  30. long long n1, n2;
  31. int count;
  32.  
  33. cin>>count;
  34.  
  35. for(int i = 1; i <= count; ++i)
  36. {
  37. int maxScore = 0, bits = 0;
  38. long long sum = 0;
  39. string newstr1 = "" , newstr2 = "";
  40. string str1 = "", str2 = "";
  41. cin>>bits>>n1>>n2;
  42.  
  43. str1 = to_string(dec2bin(n1)); //to binary
  44. str2 = to_string(dec2bin(n2));
  45.  
  46.  
  47. newstr1.resize((bits - str1.size()), '0');
  48. newstr1.append(str1);
  49. str1 = newstr1;
  50.  
  51. newstr2.resize((bits - str2.size()), '0');
  52. newstr2.append(str2);
  53. str2 = newstr2;
  54.  
  55.  
  56.  
  57. //cout<<"str1 and str2 are resp : "<<str1<<" "<<str2<<endl;
  58.  
  59. size_t str1_0s = std::count(str1.begin(), str1.end(), '0');
  60. size_t str1_1s = std::count(str1.begin(), str1.end(), '1');
  61.  
  62. size_t str2_0s = std::count(str2.begin(), str2.end(), '0');
  63. size_t str2_1s = std::count(str2.begin(), str2.end(), '1');
  64.  
  65. maxScore = std::min(str1_0s, str2_1s) + std::min(str1_1s, str2_0s);
  66. for(int i = bits -1 ; i>=bits - maxScore; --i)
  67. sum = sum + pow(2, i);
  68. cout<<sum<<endl;
  69.  
  70. }
  71. return 0;
  72. }
  73.  
  74.  
  75.  
  76.  
Success #stdin #stdout 0s 3464KB
stdin
3
3 5 4
5 0 1
4 3 7
stdout
7
16
14