找出一组数最多为 N 且按位异或为 N 的数
给定一个正整数N ,任务是找到整数对(X, Y)使得X和Y的按位异或为N并且X * Y最大,其中X和Y中的位数小于或等于N 。
例子:
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
方法:给定的问题可以通过以下观察来解决:
- 如果N的第 i位是0 ,那么X和Y的第i位必须是0或1 。为了最大化产品,将这些位设置为1 。
- 如果N的第 i位为1 ,则X或Y的第i位中的一个必须为1 ,另一个必须为0 。由于N必须具有恒定数量的设置位,因此 X 和 Y 的总和必须是恒定的。
- 如果两个数之和为常数,则当两个数之差最小时,它们的乘积最大。
根据上述观察,将两个整数X和Y初始化为 0。为了最小化X和Y之间的差异, X必须等于MSB N并且Y必须等于N – MSB N其中MSB表示最重要少量。此外,对于N中的所有0位,将X和Y中的相应位设置为1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
50 31
时间复杂度: O(log N)
辅助空间: O(N)