📌  相关文章
📜  通过一次更改 1 位或 2 位检查是否可以使给定的两个数字相等或不相等

📅  最后修改于: 2022-05-13 01:56:07.784000             🧑  作者: Mango

通过一次更改 1 位或 2 位检查是否可以使给定的两个数字相等或不相等

给定两个正整数AB ,只执行一次以下操作之一以使数字相等。

  • 将数字的第 i位更改为01
  • A中的第 i位更改为01 ,将B中的第 j位更改为01

如果可以使数字相等,则打印“是” 。否则,打印“否”

例子:

方法:给定的问题可以通过计算两个数字中设置位的差异来解决,即两个数字的位在多少位置上彼此不同。如果计数超过两个,则不可能使数字相等。

下面是上述方法的实现:

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)