Find a pair of numbers with set bit count as at most that of N and whose Bitwise XOR is N
Last Updated :
12 Oct, 2022
Given a positive integer N, the task is to find the pair of integers (X, Y) such that the Bitwise XOR of X and Y is N and X * Y is maximum where the count of bits in X and Y is less than or equal to N.
Examples:
Input: N = 10
Output: 13 7
Explanation: The case where X = 13 and Y = 7 is the most optimal choice as 13 xor 7 = 10 and 13 * 7 = 91 which is maximum possible.
Input: N = 45
Output: 50 31
Approach: The given problem can be solved using the following observations:
- If the ith bit of N is 0, then the ith bit of both X and Y must be either 0 or 1. In order to maximize the product, set such bits as 1.
- If the ith bit of N is 1, then one of the ith bits of X or Y must be 1 and the other must be 0. Since N must have a constant number of set bits, therefore the sum of X and Y must be constant.
- If the sum of two numbers is constant, their product is maximum when the difference between the two numbers is minimized.
According to the above observations, initialize two integers X and Y as 0. In order to minimize the difference between X and Y, X must be equal to the MSBN and Y must be equal to N – MSBN where MSB represents the Most Significant Bit. Also, for all the 0 bits in N, set the respective bits in both X and Y as 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG
{
static void maximizeProduct( int N)
{
int MSB = ( int )(Math.log(N) / Math.log( 2 ));
int X = 1 << MSB;
int Y = N - ( 1 << MSB);
for ( int i = 0 ; i < MSB; i++) {
if ((N & ( 1 << i))== 0 ) {
X += 1 << i;
Y += 1 << i;
}
}
System.out.println(X+ " " +Y);
}
public static void main(String[] args)
{
int N = 45 ;
maximizeProduct(N);
}
}
|
Python3
import math
def maximizeProduct(N):
MSB = ( int )(math.log2(N))
X = 1 << MSB
Y = N - ( 1 << MSB)
for i in range (MSB):
if ( not (N & ( 1 << i))):
X + = 1 << i
Y + = 1 << i
print (X, Y)
if __name__ = = "__main__" :
N = 45
maximizeProduct(N)
|
C#
using System;
class GFG {
static void maximizeProduct( int N)
{
int MSB = ( int )(Math.Log(N) / Math.Log(2));
int X = 1 << MSB;
int Y = N - (1 << MSB);
for ( int i = 0; i < MSB; i++) {
if ((N & (1 << i))==0) {
X += 1 << i;
Y += 1 << i;
}
}
Console.Write(X+ " " +Y);
}
public static void Main()
{
int N = 45;
maximizeProduct(N);
}
}
|
Javascript
<script>
function maximizeProduct(N)
{
let MSB = Math.log2(N);
let X = 1 << MSB;
let Y = N - (1 << MSB);
for (let i = 0; i < MSB; i++) {
if (!(N & (1 << i))) {
X += 1 << i;
Y += 1 << i;
}
}
document.write(X + " " + Y);
}
let N = 45;
maximizeProduct(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...