Bit Manipulation · Medium
LeetCode 1318: Minimum Flips to Make a OR b Equal to c — solution
Bit-by-bit greedy: compare each bit of a, b and c independently and add the forced number of flips. O(log C) time, at most 31 iterations, and O(1) space.
Problem in plain words
You have three positive integers a, b and c. A flip changes one bit of a or b from 0 to 1 or from 1 to 0. Find the smallest number of flips so that a | b (bitwise OR) equals c.
For example, a = 1, b = 1, c = 0 needs 2 flips: both lowest bits are 1, and both must become 0.
Intuition
OR works on each bit position separately, so the bits never interact and each position can be fixed on its own. For one position, call the bits x (from a), y (from b) and z (from c):
- If
z = 1, we need at least one ofx,yto be 1. If both are 0, flip one: 1 flip. Otherwise 0. - If
z = 0, bothxandymust be 0. Every 1 among them must be flipped: x + y flips (0, 1 or 2).
Summing these costs over all positions gives the answer. Extracting bits with & 1 and >> is the core trick from the bitwise operations lesson.
Approach
- Set
flips = 0. - While any of
a,b,cis non-zero, read the lowest bits:x = a & 1,y = b & 1,z = c & 1. - If
z == 1, add 1 whenx == 0 && y == 0. Ifz == 0, addx + y. - Shift all three right by one bit and repeat.
- Return
flips.
Dry run
Input: a = 3 (0011), b = 5 (0101), c = 10 (1010).
| Bit | a | b | c | Flips | Reason |
|---|---|---|---|---|---|
| 0 | 1 | 1 | 0 | 2 | c needs 0, both are 1 |
| 1 | 1 | 0 | 1 | 0 | already OR to 1 |
| 2 | 0 | 1 | 0 | 1 | c needs 0, b has a 1 |
| 3 | 0 | 0 | 1 | 1 | c needs 1, both are 0 |
Total: 4 flips.
Java solution
class Solution {
public int minFlips(int a, int b, int c) {
int flips = 0;
while (a != 0 || b != 0 || c != 0) {
int x = a & 1, y = b & 1, z = c & 1;
if (z == 1) {
if (x == 0 && y == 0) flips += 1; // turn one bit on
} else {
flips += x + y; // turn every 1 off
}
a >>= 1;
b >>= 1;
c >>= 1;
}
return flips;
}
}Python solution
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
flips = 0
while a or b or c:
x, y, z = a & 1, b & 1, c & 1
if z == 1:
if x == 0 and y == 0:
flips += 1 # turn one bit on
else:
flips += x + y # turn every 1 off
a >>= 1
b >>= 1
c >>= 1
return flipsComplexity
| Measure | Cost | Why |
|---|---|---|
| Time | O(log max(a, b, c)) | One iteration per bit, at most 31 for 32-bit positive integers. |
| Space | O(1) | A few integer variables. |
Edge cases and common mistakes
- Already equal: if
a | b == c, every position costs 0 and the answer is 0. - Counting only one flip when c's bit is 0: if both
aandbhave a 1 there, both must be cleared, which is 2 flips. - Stopping when c becomes 0: higher bits of
aorbmay still be 1 and must be turned off. Loop until all three are 0. - Comparing `a | b` with `c` once:
Integer.bitCount((a | b) ^ c)counts mismatched positions but undercounts the case where both bits are 1 andchas 0.
›Is there a branch-free formula?
Yes. Let wrong = (a | b) ^ c. Mismatched positions cost one flip each, plus one extra for positions where a & b & wrong is set (both bits are 1 but c wants 0). The answer is bitCount(wrong) + bitCount(a & b & wrong).
