通过一次更改 1 位或 2 位检查是否可以使给定的两个数字相等或不相等
给定两个正整数A和B ,只执行一次以下操作之一以使数字相等。
- 将数字的第 i位更改为0或1
- 将A中的第 i位更改为0或1 ,将B中的第 j位更改为0或1
如果可以使数字相等,则打印“是” 。否则,打印“否” 。
例子:
Input: A = 5, B = 15
Output: Yes
Explanation: The binary representations of the numbers 5 and 15 are 0101 and 1111 respectively.
Now, change the fourth bit of the number 5 to 1 and the second bit of the number 15 to 0.
Therefore, both the numbers become equal, i.e. 13.
Input: A = 8, B = 7
Output: No
方法:给定的问题可以通过计算两个数字中设置位的差异来解决,即两个数字的位在多少位置上彼此不同。如果计数超过两个,则不可能使数字相等。
下面是上述方法的实现:
C++
// C++ program to check if it
// is possible to make the two
// numbers equal or not
#include
using namespace std;
// Function to count the different bits
// in both numbers
void makeNumbersEqual(int A, int B)
{
// Stores the number of different bits
int diff = 0;
// Stores the binary representations of
// a and b respectively
bitset<32> ar(A), br(B);
for (int i = 0; i < 32; i++) {
if (ar[i] != br[i])
diff++;
}
if (diff <= 2)
cout << "Yes";
else
cout << "No";
}
// Driver Code
int main()
{
int A = 5, B = 15;
makeNumbersEqual(A, B);
return 0;
}
Java
// java code for the above approach
class GFG
{
// Function to count the different bits
// in both numbers
static void makeNumbersEqual(int A, int B) {
// Stores the number of different bits
int diff = 0;
// Stores the binary representations of
// a and b respectively
int[] ar = new int[32];
int[] br = new int[32];
for (int i = 0; i < 32; i++) {
ar[i] = 0;
br[i] = 0;
}
for (int i = 0; i < 32; i++) {
if ((A & (1 << i)) == 0) {
ar[i]++;
}
if ((B & (1 << i)) == 0) {
br[i]++;
}
}
for (int i = 0; i < 32; i++) {
if (ar[i] != br[i])
diff++;
}
if (diff <= 2)
System.out.print("Yes");
else
System.out.print("No");
}
// Driver Code
public static void main(String args[]) {
int A = 5, B = 15;
makeNumbersEqual(A, B);
}
}
// This code is contributed by gfgking
Python3
# Python code for the above approach
# Function to count the different bits
# in both numbers
def makeNumbersEqual(A, B):
# Stores the number of different bits
diff = 0;
# Stores the binary representations of
# a and b respectively
ar = [0] * 32
br = [0] * 32
for i in range(32):
if (A & (1 << i)):
ar[i] += 1
if (B & (1 << i)):
br[i] += 1
for i in range(32):
if (ar[i] != br[i]):
diff += 1
if (diff <= 2):
print("Yes");
else:
print("No");
# Driver Code
A = 5
B = 15;
makeNumbersEqual(A, B);
# This code is contributed by Saurabh Jaiswal
Javascript
C#
// C# code for the above approach
using System;
class GFG {
// Function to count the different bits
// in both numbers
static void makeNumbersEqual(int A, int B)
{
// Stores the number of different bits
int diff = 0;
// Stores the binary representations of
// a and b respectively
int[] ar = new int[32];
int[] br = new int[32];
for (int i = 0; i < 32; i++) {
ar[i] = 0;
br[i] = 0;
}
for (int i = 0; i < 32; i++) {
if ((A & (1 << i)) == 0) {
ar[i]++;
}
if ((B & (1 << i)) == 0) {
br[i]++;
}
}
for (int i = 0; i < 32; i++) {
if (ar[i] != br[i])
diff++;
}
if (diff <= 2)
Console.Write("Yes");
else
Console.Write("No");
}
// Driver Code
public static void Main()
{
int A = 5, B = 15;
makeNumbersEqual(A, B);
}
}
// This code is contributed by Samim Hossain Mondal.
输出
Yes
时间复杂度:O(1)
辅助空间:O(1)