Minimum Flips to Make a OR b Equal to c (LeetCode 1318): Solution and Explanation

Medium
Bit Manipulation

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

  1. Set flips = 0.
  2. While any of a, b, c is non-zero, read their lowest bits x, y, z.
  3. If z is 1, add 1 when x and y are both 0. If z is 0, add x + y.
  4. Shift a, b and c right by one and repeat.
  5. Return flips.

Step-by-step example

a = 2, b = 6, c = 5
  1. In binary a = 010, b = 110, c = 101.
  2. Bit 0: a and b are both 0 but c wants 1, so add 1 flip (flips = 1).
  3. Bit 1: a and b are both 1 but c wants 0, so both must be cleared: add 2 (flips = 3).
  4. Bit 2: b has a 1 and c wants 1, so nothing to do.
  5. 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 flips
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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.

Space

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.