text: Find a pair of numbers with set bit count as at most that of N and whose Bitwise XOR is N | C ++ program for the above approach ; Function to find the pair ( X , Y ) such that X xor Y = N and the count of set bits in X and Y is less than count of set bit in N ; Stores MSB ( Most Significant Bit ) ; Stores the value of X ; Stores the value of Y ; Traversing over all bits of N ; If ith bit of N is 0 ; Set ith bit of X to 1 ; Set ith bit of Y to 1 ; Print Answer ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void maximizeProduct ( int N ) { int MSB = ( int ) log2 ( N ) ; int X = 1 << MSB ; int Y = N - ( 1 << MSB ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ! ( N & ( 1 << i ) ) ) { X += 1 << i ; Y += 1 << i ; } } cout << X << " ▁ " << Y ; } int main ( ) { int N = 45 ; maximizeProduct ( N ) ; return 0 ; }