Techno Blender
Digitally Yours.

Equal number of 0’s and 1’s

0 38


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;

}

Time complexity: O(log(n))
Auxilairy Space: O(1)


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;

}

Time complexity: O(log(n))
Auxilairy Space: O(1)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment