从它们的总和中找到两个数字,或
给定两个整数X和Y ,任务是找到两个按位或为X且它们的和为Y的数字。如果不存在这样的整数,则打印“-1” 。
例子:
Input: X = 7, Y = 11
Output: 4 7
Explanation:
The Bitwise OR of 4 and 7 is 7 and the sum of two integers is 4 + 7 = 11, satisfy the given conditions.
Input: X = 11, Y = 7
Output: -1
朴素方法:解决给定问题的最简单方法是生成数组的所有可能对,如果存在满足给定条件的任何对,则打印这些对。否则,打印“-1” 。
时间复杂度: O(Y)
辅助空间: O(1)
高效方法:上述方法也可以通过使用位运算符的属性进行优化。考虑任意两个整数A和B ,那么两个整数之和可以表示为A + B = (A & B) + (A | B) 。现在,放置变量X和Y并将方程更改为:
=> Y = (A & B) + X
=> (A & B) = Y – X
Therefore the above observations can be deduced with this equation.
请按照以下步骤解决给定的问题:
- 如果Y的值小于X ,那么将没有解决方案,因为按位与运算总是非负的。
- 现在对于X和Y的按位表示中的第K位,如果该位在(A&B)中为“1” ,在(A | B)中为“0” ,则将没有可能的解决方案。这是因为如果两个数字的按位 AND 为1 ,那么按位 OR 也应该为1 。
- 否则,总是可以选择两个整数A和B ,它们可以计算为A = Y – X和B = X 。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
#define MaxBit 32
using namespace std;
// Function to find the two integers from
// the given sum and Bitwise OR value
int possiblePair(int X, int Y)
{
int Z = Y - X;
// Check if Z is non negative
if (Z < 0) {
cout << "-1";
return 0;
}
// Iterate through all the bits
for (int k = 0; k < MaxBit; k++) {
// Find the kth bit of A & B
int bit1 = (Z >> k) & 1;
// Find the kth bit of A | B
int bit2 = (Z >> k) & 1;
// If bit1 = 1 and bit2 = 0, then
// there will be no possible pairs
if (bit1 && !bit2) {
cout << "-1";
return 0;
}
}
// Print the possible pairs
cout << Z << ' ' << X;
return 0;
}
// Driver Code
int main()
{
int X = 7, Y = 11;
possiblePair(X, Y);
return 0;
}
Java
// Java code for above approach
import java.util.*;
class GFG{
static int MaxBit = 32;
// Function to find the two integers from
// the given sum and Bitwise OR value
static void possiblePair(int X, int Y)
{
int Z = Y - X;
// Check if Z is non negative
if (Z < 0) {
System.out.print("-1");
}
// Iterate through all the bits
for (int k = 0; k < MaxBit; k++) {
// Find the kth bit of A & B
int bit1 = (Z >> k) & 1;
// Find the kth bit of A | B
int bit2 = (Z >> k) & 1;
// If bit1 = 1 and bit2 = 0, then
// there will be no possible pairs
if (bit1 != 0 && bit2 == 0) {
System.out.print("-1");
}
}
// Print the possible pairs
System.out.print( Z + " " + X);
}
// Driver Code
public static void main(String[] args)
{
int X = 7, Y = 11;
possiblePair(X, Y);
}
}
// This code is contributed by avijitmondal1998.
Python3
# Python 3 program for the above approach
MaxBit = 32
# Function to find the two integers from
# the given sum and Bitwise OR value
def possiblePair(X, Y):
Z = Y - X
# Check if Z is non negative
if (Z < 0):
print("-1")
return 0
# Iterate through all the bits
for k in range(MaxBit):
# Find the kth bit of A & B
bit1 = (Z >> k) & 1
# Find the kth bit of A | B
bit2 = (Z >> k) & 1
# If bit1 = 1 and bit2 = 0, then
# there will be no possible pairs
if (bit1 == 1 and bit2 == 0):
print("-1")
return 0
# Print the possible pairs
print(Z, X)
return 0
# Driver Code
if __name__ == '__main__':
X = 7
Y = 11
possiblePair(X, Y)
# This code is contributed by SURENDRA_GANGWAR.
C#
//C# code for the above approach
using System;
public class GFG{
static int MaxBit = 32;
// Function to find the two integers from
// the given sum and Bitwise OR value
static void possiblePair(int X, int Y)
{
int Z = Y - X;
// Check if Z is non negative
if (Z < 0) {
Console.Write("-1");
}
// Iterate through all the bits
for (int k = 0; k < MaxBit; k++) {
// Find the kth bit of A & B
int bit1 = (Z >> k) & 1;
// Find the kth bit of A | B
int bit2 = (Z >> k) & 1;
// If bit1 = 1 and bit2 = 0, then
// there will be no possible pairs
if (bit1 != 0 && bit2 == 0) {
Console.Write("-1");
}
}
// Print the possible pairs
Console.Write( Z + " " + X);
}
// Driver Code
static public void Main (){
// Code
int X = 7, Y = 11;
possiblePair(X, Y);
}
}
// This code is contributed by Potta Lokesh
Javascript
输出:
4 7
时间复杂度: O(1)
辅助空间: O(1)