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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// 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
void maximizeProduct(int N)
{
// Stores MSB (Most Significant Bit)
int MSB = (int)log2(N);
// Stores the value of X
int X = 1 << MSB;
// Stores the value of Y
int Y = N - (1 << MSB);
// Traversing over all bits of N
for (int i = 0; i < MSB; i++) {
// If ith bit of N is 0
if (!(N & (1 << i))) {
// Set ith bit of X to 1
X += 1 << i;
// Set ith bit of Y to 1
Y += 1 << i;
}
}
// Print Answer
cout << X << " " << Y;
}
// Driver Code
int main()
{
int N = 45;
maximizeProduct(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// 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
static void maximizeProduct(int N)
{
// Stores MSB (Most Significant Bit)
int MSB = (int)(Math.log(N) / Math.log(2));
// Stores the value of X
int X = 1 << MSB;
// Stores the value of Y
int Y = N - (1 << MSB);
// Traversing over all bits of N
for (int i = 0; i < MSB; i++) {
// If ith bit of N is 0
if ((N & (1 << i))==0) {
// Set ith bit of X to 1
X += 1 << i;
// Set ith bit of Y to 1
Y += 1 << i;
}
}
// Print Answer
System.out.println(X+" "+Y);
}
// Driver Code
public static void main(String[] args)
{
int N = 45;
maximizeProduct(N);
}
}
// This code is contributed by dwivediyash
Python3
# python 3 program for the above approach
import math
# 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
def maximizeProduct(N):
# Stores MSB (Most Significant Bit)
MSB = (int)(math.log2(N))
# Stores the value of X
X = 1 << MSB
# / Stores the value of Y
Y = N - (1 << MSB)
# Traversing over all bits of N
for i in range(MSB):
# If ith bit of N is 0
if (not (N & (1 << i))):
# / Set ith bit of X to 1
X += 1 << i
# Set ith bit of Y to 1
Y += 1 << i
# Print Answer
print(X, Y)
# Driver Code
if __name__ == "__main__":
N = 45
maximizeProduct(N)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG {
// 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
static void maximizeProduct(int N)
{
// Stores MSB (Most Significant Bit)
int MSB = (int)(Math.Log(N) / Math.Log(2));
// Stores the value of X
int X = 1 << MSB;
// Stores the value of Y
int Y = N - (1 << MSB);
// Traversing over all bits of N
for (int i = 0; i < MSB; i++) {
// If ith bit of N is 0
if ((N & (1 << i))==0) {
// Set ith bit of X to 1
X += 1 << i;
// Set ith bit of Y to 1
Y += 1 << i;
}
}
// Print Answer
Console.Write(X+" "+Y);
}
// Driver Code
public static void Main()
{
int N = 45;
maximizeProduct(N);
}
}
// This code is contributed by code_hunt.
JavaScript
<script>
// JavaScript Program to implement
// 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
function maximizeProduct(N)
{
// Stores MSB (Most Significant Bit)
let MSB = Math.log2(N);
// Stores the value of X
let X = 1 << MSB;
// Stores the value of Y
let Y = N - (1 << MSB);
// Traversing over all bits of N
for (let i = 0; i < MSB; i++) {
// If ith bit of N is 0
if (!(N & (1 << i))) {
// Set ith bit of X to 1
X += 1 << i;
// Set ith bit of Y to 1
Y += 1 << i;
}
}
// Print Answer
document.write(X + " " + Y);
}
// Driver Code
let N = 45;
maximizeProduct(N);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(log N)
Auxiliary Space: O(1)
Similar Reads
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal Given an integer array arr of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Example: Input: N = 3, arr[] = {0,0,1}Output: 1Explanation: There is only one pair arr[0] and arr[1] as 0&0=0 and 0^0=0 Input: N = 4, arr[] = {1, 2, 4, 8}Output: 0Explanati
4 min read
Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1 Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that: cntAND: Count of pairs where bitwise AND of least significant bits is 1.cntOR: Count of pairs where bitwise OR of least significant bits is 1.cntXOR: Count of pairs where
7 min read
Count of pairs having bit size at most X and Bitwise OR equal to X Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X. Examples: Input: X = 6 Output: 9 Explanation: The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2
4 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2 Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value Examples: Input : arr[]={ 12, 4 , 15}Output: 2Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4
6 min read
Count of all possible values of X whose Bitwise XOR with N is greater than N Given an integer N count the number of values of X such that XâN > N, where â denotes bitwise XOR operation Examples: Input: N = 10Output: 5 Explanation: The five possible value satisfying the above condition are:1â10 = 11, 4â10 = 14, 5â10 = 15, 6â10 = 12, 7â10 = 13 Input: N = 8Output: 7Explanati
6 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Expla
9 min read