Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:

Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (aORb==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^91 <= b <= 10^91 <= c <= 10^9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to figure out the fewest changes needed to make two numbers, after combining them in a specific way, equal to a third number. The brute-force way means we're going to check every single possible combination of changes to the first two numbers.
Here's how the algorithm would work step-by-step:
def min_flips_brute_force(a_input, b_input, c_input):
minimum_flips = float('inf')
for i in range(1 << 30):
for j in range(1 << 30):
a_temp = a_input
b_temp = b_input
flips = 0
for bit_position in range(30):
a_bit = (a_input >> bit_position) & 1
b_bit = (b_input >> bit_position) & 1
c_bit = (c_input >> bit_position) & 1
a_flip = (i >> bit_position) & 1
b_flip = (j >> bit_position) & 1
if a_flip:
a_temp ^= (1 << bit_position)
flips += 1
if b_flip:
b_temp ^= (1 << bit_position)
flips += 1
# Check if after flips (a OR b) == c
if ((a_temp >> bit_position) & 1) | ((b_temp >> bit_position) & 1) != c_bit:
flips = float('inf')
break
# Store if the current flips are lower than the current minimum
minimum_flips = min(minimum_flips, flips)
if minimum_flips == float('inf'):
return -1
return minimum_flipsThe key is to examine each bit position of the numbers individually. We can determine the minimum flips needed at each bit by comparing the corresponding bits in the input numbers and the target number.
Here's how the algorithm would work step-by-step:
def minimum_flips(a, b, c):
flip_count = 0
# Iterate through the bits of the numbers
for i in range(32):
bit_a = (a >> i) & 1
bit_b = (b >> i) & 1
bit_c = (c >> i) & 1
# Check if the target bit is 1
if bit_c == 1:
# If both bits are 0, we need to flip one
if bit_a == 0 and bit_b == 0:
flip_count += 1
# Check if the target bit is 0
else:
# If either bit is 1, we need to flip it
if bit_a == 1 and bit_b == 1:
# Both must be flipped if c is 0, a and b are 1
flip_count += 2
elif bit_a == 1 or bit_b == 1:
# One of them must be flipped
flip_count += 1
return flip_count| Case | How to Handle |
|---|---|
| a, b, and c are all 0 | The algorithm should correctly return 0 since the bitwise OR of 0 and 0 is already 0. |
| a and b are 0, but c is a large positive number | The solution must accurately count the number of '1' bits in 'c' as flips needed for 'a' and 'b'. |
| a and b are large positive numbers, but c is 0 | The algorithm must accurately count the number of flips needed to turn '1' bits in 'a' and 'b' to '0'. |
| Integer Overflow: a, b, or c is close to the maximum integer value (2^31 - 1) | The bitwise operations should handle large integers correctly within the standard integer range, and we avoid intermediate results that may cause overflows during bitwise comparisons and counting. |
| a and b have many overlapping set bits, and c has few set bits | The algorithm needs to consider the case where both a and b need to be flipped if the corresponding bit in c is 0, even if either a or b originally had that bit set. |
| a, b, c are all the same number, but not 0 | If a | b == c initially, the result should be 0 flips; otherwise, we need to check if we can make a | b == c with a minimal number of flips. |
| c has a bit set to 1 where both a and b have that bit set to 0. | At least one of a or b needs a flip at that bit position. |
| c has a bit set to 0 where both a and b have that bit set to 1. | Both a and b need a flip at that bit position. |