Minimum Flips to Make a OR b Equal to c

Updated on 12 June, 2025
Minimum Flips to Make a OR b Equal to c header image

Problem Statement

Given three positive integers a, b, and c, the task is to determine the minimum number of flips required in some bits of a and b so that the result of the bitwise OR operation between a and b equals c. Here, flipping a bit refers to changing it from 1 to 0 or from 0 to 1. The goal is to align the binary representation of a OR b to exactly match that of c through the minimum possible number of bit flips.

Examples

Example 1

Input:

a = 2, b = 6, c = 5

Output:

3

Explanation:

After flips a = 1 , b = 4 , c = 5 such that (`a` OR `b` == `c`)

Example 2

Input:

a = 4, b = 2, c = 7

Output:

1

Example 3

Input:

a = 1, b = 2, c = 3

Output:

0

Constraints

  • 1 <= a <= 10^9
  • 1 <= b <= 10^9
  • 1 <= c <= 10^9

Approach and Intuition

To solve this problem, our primary goal is to align the result of a | b (bitwise OR of a and b) to match c. This can be visualized and tackled bit by bit. Here's a high-level approach:

  1. Convert numbers a, b, and c to their binary form.
  2. Compare each bit position of a, b, and the corresponding bit in c.
  3. Apply the following rules based on the bit values:
    • If c has a bit set to 1, at least one among the corresponding bits of a or b must be set to 1.
      • If both are 0, one flip is needed.
    • If c has a bit set to 0, both corresponding bits in a and b should also be 0.
      • For any bit that is 1 in either a or b, change it to 0, which might require one or two flips.
  4. Count the total flips required and that would be the answer.

Examples Breakdown

Let's elaborate the approach with the provided examples:

  • Example 1: a = 2 (binary 010), b = 6 (binary 110), c = 5 (binary 101)
    • For bit-0: a has 0, b has 0, but c needs 1. Flip either a or b's bit.
    • For bit-1: a has 1, b has 1, and c has 0. Both bits in a and b need to be flipped to 0.
    • For bit-2: a has 0, b has 1, and c has 1. No flip needed as at least one bit matches c.
    • Total flips = 3.
  • Example 2: a = 4 (binary 100), b = 2 (binary 010), c = 7 (binary 111)
    • For bit-0: a and b are both 0 while c is 1. Flip either a or b's bit.
    • For bit-1 and bit-2: Already meeting the requirement as at least one bit is 1.
    • Total flips = 1.
  • Example 3: a = 1 (binary 001), b = 2 (binary 010), c = 3 (binary 011)
    • All bits already align with c's requirements.
    • Total flips = 0.

By following the above approach, we can determine the minimum flips necessary for any given values of a, b, and c under the constraints.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int calculateMinFlips(int x, int y, int z) {
        return __builtin_popcount((x | y) ^ z) + __builtin_popcount(x & y & ((x | y) ^ z));
    }
};

In the given C++ solution to the problem "Minimum Flips to Make a OR b Equal to c", the function calculateMinFlips computes the number of bit flips required to modify the bitwise OR of two integers, x and y, so that it equals a third integer, z.

Understand the approach by looking at the components of the function:

  • x | y computes the bitwise OR of x and y.
  • (x | y) ^ z performs a bitwise XOR between the result of x | y and z. This XOR operation pinpoints the bits that are different between x | y and z.
  • __builtin_popcount((x | y) ^ z) counts the number of bits set to 1 in the result of the XOR operation, representing the number of differing bits between x | y and z which must be flipped.
  • x & y & ((x | y) ^ z) identifies the positions where both x and y have bits set that need to be flipped to make (x | y) equal to z.
  • __builtin_popcount(x & y & ((x | y) ^ z)) counts these specific positions which need correction.

By combining these two results, the function accurately calculates the number of flips required. This method is efficient and leverages bit manipulation functions available in C++ for quick computation.

java
class Solution {
    public int calculateMinFlips(int x, int y, int z) {
        return Integer.bitCount((x | y) ^ z) + Integer.bitCount(x & y & ((x | y) ^ z));   
    }
}

In this Java solution, you are given a method that calculates the minimum number of flips required to make the bitwise OR of two integers ( x ) and ( y ) equal to another integer ( z ). The implementation makes efficient use of bitwise operations.

  • The calculateMinFlips method takes three integers x, y, and z as inputs.
  • Utilize Integer.bitCount() method, which counts the number of one-bits in the binary representation of the passed integer.
  • Compute (x | y) ^ z to determine the bits that differ when comparing the bitwise OR of x and y to z.
  • Compute x & y & ((x | y) ^ z) to determine bits where both x and y are set and yet differ from z.
  • The sum of the bit counts of these two operations gives the total number of flips needed.

This approach ensures optimal computation by directly using bitwise operators to identify the specific bits that require flipping. Adjust x and y such that their OR operation's outcome matches z through minimal changes.

python
class Solution:
    def calculateMinFlips(self, num1: int, num2: int, num3: int) -> int:
        return bin((num1 | num2) ^ num3).count("1") + bin(num1 & num2 & ((num1 | num2) ^ num3)).count("1")

    def calculateMinFlipsNew(self, num1: int, num2: int, num3: int) -> int:
        return ((num1 | num2) ^ num3).bit_count() + (num1 & num2 & ((num1 | num2) ^ num3)).bit_count()

This solution involves implementing two methods in Python to determine the minimum number of bit flips required to make the bitwise OR of two numbers (num1 or num2) equal to a third number (num3). Here is an overview of how each method works:

  • calculateMinFlips:

    • Calculate the XOR of the OR of num1 and num2 with num3. This XOR operation identifies positions where bits differ between num1 | num2 and num3.
    • Count the number of '1's in this XOR result using the bin() function and count("1"). This count gives the number of bits that need to be flipped.
    • Additionally, calculate the bitwise AND of num1 and num2 with the XOR result to find bits that are set in both num1 and num2 and need to be flipped. Count the '1's in this result similarly.
    • Sum the counts from the XOR and AND operations to get the total number of flips.
  • calculateMinFlipsNew:

    • Similar to the first method but uses the bit_count() function introduced in Python 3.10 for determining the number of bits that are '1'. The bit_count() function is used directly on the XOR and AND results, making the code cleaner and possibly more efficient.

Both methods provide an efficient way to compute the minimal flips needed by utilizing bitwise operations and ensure performance with in-built Python methods. The choice between the two might depend on the Python version in use due to the bit_count() function's availability.

Comments

No comments yet.