In this Leetcode Natural XOR challenge problem solution You are given an integer C such that the XOR of two integers (A, B) is C. In short A ⊕ B = C (⊕ denotes the bitwise XOR operation).

Out of all possible pairs of A and B, you must find two integers such that their product is maximum. 

Let us define L(A) as the length of A in its binary representation. Then, L(A) <= L(C) and L(B) <= L(C).



HackerEarth A XOR challenge problem solution


HackerEarth A XOR challenge problem solution.

#include<bits/stdc++.h>
#include<climits>
using namespace std;
 
#define debug(x,y) cout<<(#x)<<" " <<(#y)<<" is " << (x) <<" "<< (y) << endl
#define watch(x) cout<<(#x)<<" is " << (x) << endl 
#define fast ios_base::sync_with_stdio(false)
#define fie(i,a,b) for(i=a;i<b;i++)
#define MOD 1000000007
#define mod 998244353
#define PB push_back 
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ll long long 
#define lld long long int
#define ALL(x) (x).begin(),(x).end()
 
typedef vector<lld> vi;
typedef vector<vector<lld>> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<pair<lld, lld>> vpi;
typedef long long LL;
 
string convert(lld n) {
    string s = "";
    while (n > 0) {
        if (n % 2 == 1) s = "1" + s;
        else s = "0" + s;
        n /= 2;
    }
    return s;
}
 
 
lld convertBack(string s) {
    lld n = 0 , p = 1;
    for (lld i = s.length() - 1; i >= 0; i--) {
        n += ((s[i] - '0') * p);
        p *= 2;
    }
    return n;
}
 
int main() {
    fast;
    cin.tie(0);
    lld n, i;
    cin >> n;
    string s = convert(n);
    string a = "" , b = "";
    bool first = false;
    for (i = 0; i < s.length(); i++) { 
        if (s[i] == '0') {
            a += "1";
            b += "1";
        }
        else {
            if (first) {
                a += "0";
                b += "1";
            }
            else {
                a += "1";
                b += "0";
                first = true;
            }
        }
    }
    lld n1 = convertBack(a);
    lld n2 = convertBack(b);
    cout << n1*n2 << endl;
}


Second solution

c = int(input())
ans = 0
for a in range(c + 1):
    ans = max(ans, a * (a ^ c))
print(ans)