Beingamanforever

Hackerrank Maximising xor

Nov 12th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-11-12 20:08:38
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     int l, r;
  28.     cin >> l >> r;
  29.     // we have to find the maximum value of (x^y) where l <= x <= y <= r
  30.     // observe the msb of l^r is always fixed, we can fill all other bits as 1
  31.     int s = l ^ r;
  32.     int ans = 1;
  33.     while (s)
  34.     {
  35.         s >>= 1;
  36.         ans <<= 1;
  37.     }
  38.     // 2^msb - 1
  39.     cout << ans - 1 << endl;
  40.     return;
  41. }
  42.  
  43. signed main()
  44. {
  45.     NeedForSpeed;
  46.     int t = 1;
  47.     // cin >> t;
  48.     while (t--)
  49.     {
  50.         solve();
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment