Minimum Flips to Make a OR b Equal to c (LeetCode 1318): Solution and Explanation
Count the fewest single-bit flips on a and b so that a | b equals c.
Intuition
OR has no carries. Bit k of a | b depends only on bit k of a and bit k of b, so every column can be fixed without touching the others, and the answer is the sum of the column costs.
If c has a 1 in a column, a | b only needs one 1 there. When a and b both have 0, one flip on either is enough; otherwise the column is already fine.
If c has a 0 in a column, a | b must be 0, which forces both a and b to 0. Every 1 in that column costs one flip, so the cost is the a bit plus the b bit, which can be 2.
Approach: Per-bit cost with right shifts
- Set
flips = 0. - While any of
a,b,cis non-zero, read their lowest bitsx,y,z. - If
zis 1, add 1 whenxandyare both 0. Ifzis 0, addx + y. - Shift
a,bandcright by one and repeat. - Return
flips.
Step-by-step example
a = 2, b = 6, c = 5
- In binary a = 010, b = 110, c = 101.
- Bit 0: a and b are both 0 but c wants 1, so add 1 flip (flips = 1).
- Bit 1: a and b are both 1 but c wants 0, so both must be cleared: add 2 (flips = 3).
- Bit 2: b has a 1 and c wants 1, so nothing to do.
- All three numbers are now 0 after shifting, so return 3.
Code: Python, Java, C++
1class Solution:
2 def minFlips(self, a: int, b: int, c: int) -> int:
3 flips = 0
4 while a or b or c:
5 x, y, z = a & 1, b & 1, c & 1
6 if z == 1:
7 flips += 1 if x == 0 and y == 0 else 0
8 else:
9 flips += x + y
10 a >>= 1
11 b >>= 1
12 c >>= 1
13 return flips1class Solution {
2 public int minFlips(int a, int b, int c) {
3 int flips = 0;
4 while (a != 0 || b != 0 || c != 0) {
5 int x = a & 1, y = b & 1, z = c & 1;
6 if (z == 1) {
7 flips += (x == 0 && y == 0) ? 1 : 0;
8 } else {
9 flips += x + y;
10 }
11 a >>= 1;
12 b >>= 1;
13 c >>= 1;
14 }
15 return flips;
16 }
17}1class Solution {
2public:
3 int minFlips(int a, int b, int c) {
4 int flips = 0;
5 while (a != 0 || b != 0 || c != 0) {
6 int x = a & 1, y = b & 1, z = c & 1;
7 if (z == 1) {
8 flips += (x == 0 && y == 0) ? 1 : 0;
9 } else {
10 flips += x + y;
11 }
12 a >>= 1;
13 b >>= 1;
14 c >>= 1;
15 }
16 return flips;
17 }
18};Time and space complexity
O(log max(a, b, c))
The loop runs once per bit of the largest input, at most 30 times for values up to 10^9.
O(1)
Only the counter and the three shifting numbers are kept.
Other approaches
Popcount formula (time O(1), space O(1))
Bits where (a | b) ^ c is 1 need fixing. Count them with popcount, then add popcount(a & b & ~c) for columns where both a and b hold a 1 that c rejects, since those need a second flip.
Loop over 31 fixed positions (time O(31), space O(1))
Instead of shifting, test bit i of each number with (v >> i) & 1 for i from 0 to 30. Same logic, a fixed number of passes.
Common mistakes
- Charging only 1 flip when c has a 0 and both a and b have a 1. Both bits must be cleared, which costs 2.
- Charging 2 flips when c has a 1 and both a and b have a 0. One 1 is enough for OR.
- Stopping when a and b reach 0 while c still has higher bits set. Those columns each need one flip.
Frequently asked questions
Why can the columns be handled separately?
Bitwise OR has no carries, so changing one bit of a or b only affects that same bit of a | b. No column's choice changes any other column's cost.
Which bit should be flipped when c wants a 1 and both are 0?
Either one. The problem only asks for the count, and both choices cost exactly one flip.
Is there a one-line formula?
Yes: popcount((a | b) ^ c) + popcount(a & b & ~c). The first term counts wrong columns, the second adds the extra flip for columns where both a and b must be cleared.