Given two numbers a and b. Find the minimum number of steps required to make the number of the set (1) and unset(0) bits equal in a, b, and a ^ b where you can perform the following operations:
- convert a = a ^ p, p is some number
- convert b = b ^ q, q is some number
Examples:
Input: a = 10, b = 12
output: 0
Explanation:
- in a, number of bits containing 0 = 2 and number of bits containing 1 = 2
- in b, number of bits containing 0 = 2 and number of bits containing 1 = 2
x = a ^ b = 0110, So no conversion is needed clearly, no p, q is needed. Therefore, the minimum number of steps is 0
Input : a = 0, b = 1
Output: 1
Explanation : take p = 1010, q = 1011 then we will get desired numbers a = a ^ p and b by b = b ^ q. So, one conversion is needed, hence the minimum number of steps here is 1
Approach: This can be solved with the following idea:
If the number of bits with 0 in a that of 1 in a, b, and x then a minimum number of steps is 0, so no conversion is needed. In all other cases, observe that if we can convert a to 1010 and b to 1100 or any kind of this where a, b, a ^ b will follow the property mentioned in the question, then we are done as we got a = 1010, b = 1100. Clearly, the minimum number of steps will be 1 here.
Steps involved in the implementation of code:
- Calculate the total number of bits to be considered.
- Calculate the number of set bits in a, b, and a ^ b.
- After that calculate unset bits in a, b, and a ^ b.
- Check it all of them are equal to 3 of them.
- If equal, print 0.
- If not, print -1.
Below is the implementation for the above approach:
// C++ code for the above approach #include <bits/stdc++.h> using namespace std;
// Kernighan's algorithm to // find number of set bits int numberOfBits( int n)
{ int count = 0;
while (n != 0) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
} // Function to find the minimum // number of steps int maxNumOfZeroAndOne( int a, int b)
{ // write your code here
int i, tnob = -1, nozia, nozib, nooia, nooib, nozix,
nooix;
// Calculating number of bits to be
// considered
for (i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0)
|| ((b & (1 << i)) != 0)) {
tnob = i + 1;
break ;
}
}
// Number of one's in a
nooia = numberOfBits(a);
// Number of one's in b
nooib = numberOfBits(b);
// Number of one's in x
nooix = numberOfBits(a ^ b);
// Number of zero's in a
nozia = tnob - nooia;
// Number of zero's in b
nozib = tnob - nooib;
// Number of zero's in x
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib
&& nozix == nooix) {
return 0;
}
else {
return 1;
}
} // Driver code int main()
{ int a = 12, b = 10;
// Function call
cout << maxNumOfZeroAndOne(a, b);
return 0;
} |
// Java code for the above approach import java.io.*;
class GFG {
// Kernighan's algorithm to find number of set bits
static int numberOfBits( int n)
{
int count = 0 ;
while (n != 0 ) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
// Function to find the minimum number of steps
static int maxNumOfZeroAndOne( int a, int b)
{
// write your code here
int i, tnob = - 1 , nozia, nozib, nooia, nooib, nozix,
nooix;
// Calculating number of bits to be considered
for (i = 31 ; i >= 0 ; i--) {
if (((a & ( 1 << i)) != 0 )
|| ((b & ( 1 << i)) != 0 )) {
tnob = i + 1 ;
break ;
}
}
// Number of one's in a
nooia = numberOfBits(a);
// Number of one's in b
nooib = numberOfBits(b);
// Number of one's in x
nooix = numberOfBits(a ^ b);
// Number of zero's in a
nozia = tnob - nooia;
// Number of zero's in b
nozib = tnob - nooib;
// Number of zero's in x
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib
&& nozix == nooix) {
return 0 ;
}
else {
return 1 ;
}
}
public static void main(String[] args)
{
int a = 12 , b = 10 ;
// Function call
System.out.println(maxNumOfZeroAndOne(a, b));
}
} // This code is contributed by sankar. |
# Python code for the above approach import math
def numberOfBits(n):
# Kernighan's algorithm to find number of set bits
count = 0
while n ! = 0 :
rmsb = n & - n
n = n - rmsb
count + = 1
return count
def maxNumOfZeroAndOne(a, b):
# Function to find the minimum number of steps
i = tnob = - 1
nozia = nozib = nooia = nooib = nozix = nooix = 0
# Calculating number of bits to be considered
for i in range ( 31 , - 1 , - 1 ):
if ((a & ( 1 << i)) ! = 0 ) or ((b & ( 1 << i)) ! = 0 ):
tnob = i + 1
break
# Number of one's in a
nooia = numberOfBits(a)
# Number of one's in b
nooib = numberOfBits(b)
# Number of one's in x
nooix = numberOfBits(a ^ b)
# Number of zero's in a
nozia = tnob - nooia
# Number of zero's in b
nozib = tnob - nooib
# Number of zero's in x
nozix = tnob - nooix
if nozia = = nooia and nozib = = nooib and nozix = = nooix:
return 0
else :
return 1
if __name__ = = '__main__' :
a = 12
b = 10
# Function call
print (maxNumOfZeroAndOne(a, b))
# This code is contributed by Vikas Bishnoi |
// C# code for the above approach using System;
public class GFG {
// Kernighan's algorithm to find number of set bits
static int numberOfBits( int n)
{
int count = 0;
while (n != 0) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
// Function to find the minimum number of steps
static int maxNumOfZeroAndOne( int a, int b)
{
// write your code here
int i, tnob = -1, nozia, nozib, nooia, nooib, nozix,
nooix;
// Calculating number of bits to be considered
for (i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0)
|| ((b & (1 << i)) != 0)) {
tnob = i + 1;
break ;
}
}
// Number of one's in a
nooia = numberOfBits(a);
// Number of one's in b
nooib = numberOfBits(b);
// Number of one's in x
nooix = numberOfBits(a ^ b);
// Number of zero's in a
nozia = tnob - nooia;
// Number of zero's in b
nozib = tnob - nooib;
// Number of zero's in x
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib
&& nozix == nooix) {
return 0;
}
else {
return 1;
}
}
static public void Main()
{
// Code
int a = 12, b = 10;
// Function call
Console.WriteLine(maxNumOfZeroAndOne(a, b));
}
} // This code is contributed by karthik. |
// JavaScript code for the above approach // Kernighan's algorithm to find number of set bits function numberOfBits(n) {
let count = 0; while (n != 0) {
let rmsb = n & -n; n = n - rmsb; count++; } return count;
} // Function to find the minimum number of steps function maxNumOfZeroAndOne(a, b) {
let tnob = -1, nozia, nozib, nooia, nooib, nozix, nooix; // Calculating number of bits to be considered for (let i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0) ||
((b & (1 << i)) != 0)) { tnob = i + 1; break ;
} } // Number of one's in a nooia = numberOfBits(a); // Number of one's in b nooib = numberOfBits(b); // Number of one's in x nooix = numberOfBits(a ^ b); // Number of zero's in a nozia = tnob - nooia; // Number of zero's in b nozib = tnob - nooib; // Number of zero's in x nozix = tnob - nooix; if (nozia == nooia && nozib == nooib &&
nozix == nooix) { return 0;
} else {
return 1;
} } // Function call let a = 12, b = 10; console.log(maxNumOfZeroAndOne(a, b)); |
0
Time complexity: O(log(n))
Auxilairy Space: O(1)