Open In App

Check if it is possible to make given two numbers equal or not by changing 1 bit or 2 bits once

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers A and B, perform one of the following operations only once to make the numbers equal. 

  • Change the ith bit of a number to 0 or 1
  • Change the ith bit to 0 or 1 in A and jth bit to 0 or 1 in B

If it’s possible to make the numbers equal, then print “Yes”. Otherwise, print “No”.

Examples:

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

 

Approach: The given problem can be solved by counting the difference of set bits in both numbers i.e. in how many positions the bits of both the numbers are different from each other. If the count is more than two then it is not possible to make the numbers equal.  

Below is the implementation of the above approach:

C++




// C++ program to check if it
// is possible to make the two
// numbers equal or not
#include <bits/stdc++.h>
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


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.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to count the different bits
        // in both numbers
        function makeNumbersEqual(A, B)
        {
         
            // Stores the number of different bits
            let diff = 0;
 
            // Stores the binary representations of
            // a and b respectively
            let ar = new Array(32).fill(0), br = new Array(32).fill(0);
 
            for (let i = 0; i < 32; i++) {
                if (A & (1 << i)) {
                    ar[i]++;
                }
 
                if (B & (1 << i)) {
                    br[i]++;
                }
            }
 
            for (let i = 0; i < 32; i++) {
                if (ar[i] != br[i])
                    diff++;
            }
 
            if (diff <= 2)
                document.write("Yes");
            else
                document.write("No");
        }
 
        // Driver Code
        let A = 5, B = 15;
        makeNumbersEqual(A, B);
 
       // This code is contributed by Potta Lokesh
    </script>


Output

Yes

 

Time Complexity: If we assume that numbers are be only 32 bit long then time complexity will be O(1). but numbers can be of N bits long….so time complexity in that case will be O(N).
Auxiliary Space: If numbers are N bit long, then space required would be O(N) to store their binary representation . If maximum lengths of binary representation of  A and B is 32 then only Space complexity would be O(1).

Efficient Approach:   As we have to count number of different bits in A and B…..So we will use XOR property here and will generate a number that has only bits as set which are different in A and B both.

so we will do X=A^B, then set bits in X will be those bits which are different in A and B.

Now our work is to count number of set bits in X.

C++




// C++ program to check if it
// is possible to make the two
// numbers equal or not
 
#include <bits/stdc++.h>
using namespace std;
 
string makeNumbersEqual(int A, int B){
 
  // Find Xor of A and B
  int X = A ^ B;
 
  // variable to count total set bits in X.
  int count_setBits = 0;
 
  // loop to check whether X has more than 2
  // set bits or not.If count_setBits is
  // more than 2 then return No.
  while (X > 0){
    if ((X & 1) == 1 )
      count_setBits += 1;
 
    // checking count of set bits at each iteration
    if (count_setBits > 2)
      return "No";
 
    // making X half at each time.
    X /= 2;
  }
  return "Yes";
}
 
// Driver Code
int main()
{
  int A = 5, B = 15;
  cout << makeNumbersEqual(A, B);
  return 0;
}
 
// This code is contributed by jana_sayantan.


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  static String makeNumbersEqual(int A, int B){
 
    // Find Xor of A and B
    int X = A ^ B;
 
    // variable to count total set bits in X.
    int count_setBits = 0;
 
    // loop to check whether X has more than 2
    // set bits or not.If count_setBits is
    // more than 2 then return No.
    while (X > 0){
      if ((X & 1) == 1 )
        count_setBits += 1;
 
      // checking count of set bits at each iteration
      if (count_setBits > 2)
        return "No";
 
      // making X half at each time.
      X /= 2;
    }
    return "Yes";
  }
 
  // Driver Code
  public static void main (String[] args) {
 
    int A = 5;
    int B = 15;
    System.out.print(makeNumbersEqual(A, B));
  }
}
 
// This code is contributed by code_hunt.


Python3




# Python code for the above approach
# Function to count the different bits
# in both numbers
 
 
def makeNumbersEqual(A, B):
    # Find Xor of A and B
    X = A ^ B
    # variable to count total set bits in X.
    count_setBits = 0
    # loop to check whether X has more than 2
    # set bits or not.If count_setBits is
    # more than 2 then return No.
    while X > 0:
        if X & 1 == 1:
            count_setBits += 1
            # checking count of set bits at each iteration
            if count_setBits > 2:
                return 'No'
        # making X half at each time.
        X //= 2
    return 'Yes'
 
 
# Driver Code.
A = 5
B = 15
print(makeNumbersEqual(A, B))
 
'''Code is written by Rajat Kumar'''


C#




// C# program for the above approach
using System;
 
public class GFG
{
  static string makeNumbersEqual(int A, int B)
  {
 
    // Find Xor of A and B
    int X = A ^ B;
 
    // variable to count total set bits in X.
    int count_setBits = 0;
 
    // loop to check whether X has more than 2
    // set bits or not.If count_setBits is
    // more than 2 then return No.
    while (X > 0) {
      if ((X & 1) == 1)
        count_setBits += 1;
 
      // checking count of set bits at each iteration
      if (count_setBits > 2)
        return "No";
 
      // making X half at each time.
      X /= 2;
    }
    return "Yes";
  }
  public static void Main(string[] args)
  {
 
    int A = 5;
    int B = 15;
    Console.WriteLine(makeNumbersEqual(A, B));
  }
}
 
// This code is contributed by phasing17


Javascript




<script>
 
// JavaScript code to implement the above approach
 
function makeNumbersEqual(A, B)
{
 
    // Find Xor of A and B
    let X = A ^ B
     
    // variable to count total set bits in X.
    let count_setBits = 0
     
    // loop to check whether X has more than 2
    // set bits or not.If count_setBits is
    // more than 2 then return No.
    while(X > 0)
    {
        if(X & 1 == 1)
        {
            count_setBits += 1
             
            // checking count of set bits at each iteration
            if(count_setBits > 2)
                return 'No'
        }
         
        // making X half at each time.a
        X = Math.floor(X/2)
    }
    return 'Yes'
}
 
// Driver Code.
let A = 5
let B = 15
document.write(makeNumbersEqual(A, B))
 
// This code is contributed by shinjanpatra
 
</script>


Output

Yes

Time complexity: No matter how lengthy A and B are, Time complexity would be O(log(max(A,B))) in worst case.

In avg case, time complexity would be O(1) as we need to count only 2 set bits.

Space Complexity:  O(1) Always.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads