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
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++
#include <bits/stdc++.h>
using namespace std;
void makeNumbersEqual( int A, int B)
{
int diff = 0;
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" ;
}
int main()
{
int A = 5, B = 15;
makeNumbersEqual(A, B);
return 0;
}
|
Java
class GFG
{
static void makeNumbersEqual( int A, int B) {
int diff = 0 ;
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" );
}
public static void main(String args[]) {
int A = 5 , B = 15 ;
makeNumbersEqual(A, B);
}
}
|
Python3
def makeNumbersEqual(A, B):
diff = 0 ;
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" );
A = 5
B = 15 ;
makeNumbersEqual(A, B);
|
C#
using System;
class GFG {
static void makeNumbersEqual( int A, int B)
{
int diff = 0;
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" );
}
public static void Main()
{
int A = 5, B = 15;
makeNumbersEqual(A, B);
}
}
|
Javascript
<script>
function makeNumbersEqual(A, B)
{
let diff = 0;
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" );
}
let A = 5, B = 15;
makeNumbersEqual(A, B);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
string makeNumbersEqual( int A, int B){
int X = A ^ B;
int count_setBits = 0;
while (X > 0){
if ((X & 1) == 1 )
count_setBits += 1;
if (count_setBits > 2)
return "No" ;
X /= 2;
}
return "Yes" ;
}
int main()
{
int A = 5, B = 15;
cout << makeNumbersEqual(A, B);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static String makeNumbersEqual( int A, int B){
int X = A ^ B;
int count_setBits = 0 ;
while (X > 0 ){
if ((X & 1 ) == 1 )
count_setBits += 1 ;
if (count_setBits > 2 )
return "No" ;
X /= 2 ;
}
return "Yes" ;
}
public static void main (String[] args) {
int A = 5 ;
int B = 15 ;
System.out.print(makeNumbersEqual(A, B));
}
}
|
Python3
def makeNumbersEqual(A, B):
X = A ^ B
count_setBits = 0
while X > 0 :
if X & 1 = = 1 :
count_setBits + = 1
if count_setBits > 2 :
return 'No'
X / / = 2
return 'Yes'
A = 5
B = 15
print (makeNumbersEqual(A, B))
|
C#
using System;
public class GFG
{
static string makeNumbersEqual( int A, int B)
{
int X = A ^ B;
int count_setBits = 0;
while (X > 0) {
if ((X & 1) == 1)
count_setBits += 1;
if (count_setBits > 2)
return "No" ;
X /= 2;
}
return "Yes" ;
}
public static void Main( string [] args)
{
int A = 5;
int B = 15;
Console.WriteLine(makeNumbersEqual(A, B));
}
}
|
Javascript
<script>
function makeNumbersEqual(A, B)
{
let X = A ^ B
let count_setBits = 0
while (X > 0)
{
if (X & 1 == 1)
{
count_setBits += 1
if (count_setBits > 2)
return 'No'
}
X = Math.floor(X/2)
}
return 'Yes'
}
let A = 5
let B = 15
document.write(makeNumbersEqual(A, B))
</script>
|
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
Share your thoughts in the comments
Please Login to comment...