Equal number of 0’s and 1’s
Last Updated :
05 May, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int numberOfBits( int n)
{
int count = 0;
while (n != 0) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
int maxNumOfZeroAndOne( int a, int b)
{
int i, tnob = -1, nozia, nozib, nooia, nooib, nozix,
nooix;
for (i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0)
|| ((b & (1 << i)) != 0)) {
tnob = i + 1;
break ;
}
}
nooia = numberOfBits(a);
nooib = numberOfBits(b);
nooix = numberOfBits(a ^ b);
nozia = tnob - nooia;
nozib = tnob - nooib;
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib
&& nozix == nooix) {
return 0;
}
else {
return 1;
}
}
int main()
{
int a = 12, b = 10;
cout << maxNumOfZeroAndOne(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfBits( int n)
{
int count = 0 ;
while (n != 0 ) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
static int maxNumOfZeroAndOne( int a, int b)
{
int i, tnob = - 1 , nozia, nozib, nooia, nooib, nozix,
nooix;
for (i = 31 ; i >= 0 ; i--) {
if (((a & ( 1 << i)) != 0 )
|| ((b & ( 1 << i)) != 0 )) {
tnob = i + 1 ;
break ;
}
}
nooia = numberOfBits(a);
nooib = numberOfBits(b);
nooix = numberOfBits(a ^ b);
nozia = tnob - nooia;
nozib = tnob - nooib;
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 ;
System.out.println(maxNumOfZeroAndOne(a, b));
}
}
|
Python3
import math
def numberOfBits(n):
count = 0
while n ! = 0 :
rmsb = n & - n
n = n - rmsb
count + = 1
return count
def maxNumOfZeroAndOne(a, b):
i = tnob = - 1
nozia = nozib = nooia = nooib = nozix = nooix = 0
for i in range ( 31 , - 1 , - 1 ):
if ((a & ( 1 << i)) ! = 0 ) or ((b & ( 1 << i)) ! = 0 ):
tnob = i + 1
break
nooia = numberOfBits(a)
nooib = numberOfBits(b)
nooix = numberOfBits(a ^ b)
nozia = tnob - nooia
nozib = tnob - nooib
nozix = tnob - nooix
if nozia = = nooia and nozib = = nooib and nozix = = nooix:
return 0
else :
return 1
if __name__ = = '__main__' :
a = 12
b = 10
print (maxNumOfZeroAndOne(a, b))
|
C#
using System;
public class GFG {
static int numberOfBits( int n)
{
int count = 0;
while (n != 0) {
int rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
static int maxNumOfZeroAndOne( int a, int b)
{
int i, tnob = -1, nozia, nozib, nooia, nooib, nozix,
nooix;
for (i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0)
|| ((b & (1 << i)) != 0)) {
tnob = i + 1;
break ;
}
}
nooia = numberOfBits(a);
nooib = numberOfBits(b);
nooix = numberOfBits(a ^ b);
nozia = tnob - nooia;
nozib = tnob - nooib;
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib
&& nozix == nooix) {
return 0;
}
else {
return 1;
}
}
static public void Main()
{
int a = 12, b = 10;
Console.WriteLine(maxNumOfZeroAndOne(a, b));
}
}
|
Javascript
function numberOfBits(n) {
let count = 0;
while (n != 0) {
let rmsb = n & -n;
n = n - rmsb;
count++;
}
return count;
}
function maxNumOfZeroAndOne(a, b) {
let tnob = -1, nozia, nozib, nooia, nooib, nozix,
nooix;
for (let i = 31; i >= 0; i--) {
if (((a & (1 << i)) != 0) ||
((b & (1 << i)) != 0)) {
tnob = i + 1;
break ;
}
}
nooia = numberOfBits(a);
nooib = numberOfBits(b);
nooix = numberOfBits(a ^ b);
nozia = tnob - nooia;
nozib = tnob - nooib;
nozix = tnob - nooix;
if (nozia == nooia && nozib == nooib &&
nozix == nooix) {
return 0;
} else {
return 1;
}
}
let a = 12,
b = 10;
console.log(maxNumOfZeroAndOne(a, b));
|
Time complexity: O(log(n))
Auxilairy Space: O(1)
Share your thoughts in the comments
Please Login to comment...