Browse curriculum

Minimum Flips to Make a OR b Equal to c (LeetCode 1318)

Minimum Flips to Make a OR b Equal to c (LeetCode 1318): count the fewest single-bit flips in a and b so that a OR b equals c. Each bit position is independent: where c has 1, flip once if a and b both have 0; where c has 0, flip every 1 in a and b.

Problem Understanding

Minimum Flips to Make a OR b Equal to c (LeetCode 1318): given three positive integers a, b and c, return the minimum number of bit flips in a and b that make (a OR b) == c. A flip changes a single bit from 1 to 0 or from 0 to 1.

Examples: a = 2, b = 6, c = 5 → 3; a = 4, b = 2, c = 7 → 1; a = 1, b = 2, c = 3 → 0.

The Intuition: One Column at a Time

Write a, b and c in binary, one above the other. Each column can be fixed without touching the others.

  1. c has 1 in this column: a OR b needs a 1. If a and b both have 0, flip one of them: 1 flip. Otherwise nothing.
  2. c has 0: both a and b must have 0. Flip every 1 among them: x + y flips.
  3. Shift all three right and repeat until they are all 0.

It is the same bit-by-bit loop as Number of 1 Bits, run on three numbers at once. More problems in the bit manipulation pattern use the same shift-and-mask step.

Minimum Flips to Make a OR b Equal to c Visualization

BIT 0 ON THE RIGHT · BOTTOM ROW = FLIPS EACH BIT COST

START AT BIT 0

a = 2

0
0
1
0

b = 6

0
1
1
0

c = 5

0
1
0
1

flips

3

2

1

0

bit

:

flips

0

Answer

:

 

a = 2, b = 6, c = 5. Walk the bits from the right while any of them is non-zero

Next

Bit 0: c has 1 but a and b are both 0. Flip one of them: +1 (1)

Free preview: the first 3 of 5 steps

a, b, c

a, b and c are drawn in binary with bit 0 on the right, and the walk moves right to left. The bottom row records what each column cost: green for no flip, red for one or two. Try Clear both, where every column costs two flips.

Minimum Flips to Make a OR b Equal to c Dry Run

Input: a = 2 (010), b = 6 (110), c = 5 (101).

Bit a b c Rule Flips Total
0 0 0 1 c is 1, a and b are 0 1 1
1 1 1 0 c is 0, clear both 1s 2 3
2 0 1 1 c is 1, b already has 1 0 3

Answer: 3.

Minimum Flips to Make a OR b Equal to c LeetCode Solution

Mask the lowest bit of each number, apply the two rules, shift, repeat.

Minimum Flips to Make a OR b Equal to c Code
1var minFlips = function(a, b, c) {
2 let flips = 0;
3 while (a || b || c) {
4 const x = a & 1, y = b & 1, z = c & 1;
5 if (z === 1) {
6 if (x === 0 && y === 0) {
7 flips++;
8 }
9 } else {
10 flips += x + y;
11 }
12 a >>= 1;
13 b >>= 1;
14 c >>= 1;
15 }
16 return flips;
17};

Minimum Flips to Make a OR b Equal to c in Python, Java, C++, TypeScript and C

Minimum Flips to Make a OR b Equal to c in Python
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
            else:
                flips += x + y
            a, b, c = a >> 1, b >> 1, c >> 1
        return flips
Minimum Flips to Make a OR b Equal to c in Java
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++;
                }
            } else {
                flips += x + y;
            }
            a >>= 1;
            b >>= 1;
            c >>= 1;
        }
        return flips;
    }
}
Minimum Flips to Make a OR b Equal to c in C++
class Solution {
public:
    int minFlips(int a, int b, int c) {
        int flips = 0;
        while (a || b || c) {
            int x = a & 1, y = b & 1, z = c & 1;
            if (z == 1) {
                if (x == 0 && y == 0) {
                    flips++;
                }
            } else {
                flips += x + y;
            }
            a >>= 1;
            b >>= 1;
            c >>= 1;
        }
        return flips;
    }
};
Minimum Flips to Make a OR b Equal to c in TypeScript
function minFlips(a: number, b: number, c: number): number {
    let flips = 0;
    while (a || b || c) {
        const x = a & 1, y = b & 1, z = c & 1;
        if (z === 1) {
            if (x === 0 && y === 0) {
                flips++;
            }
        } else {
            flips += x + y;
        }
        a >>= 1;
        b >>= 1;
        c >>= 1;
    }
    return flips;
}
Minimum Flips to Make a OR b Equal to c in C
int minFlips(int a, int b, int c) {
    int flips = 0;
    while (a || b || c) {
        int x = a & 1, y = b & 1, z = c & 1;
        if (z == 1) {
            if (x == 0 && y == 0) {
                flips++;
            }
        } else {
            flips += x + y;
        }
        a >>= 1;
        b >>= 1;
        c >>= 1;
    }
    return flips;
}

Edge Cases & Common Mistakes

  • Counting one flip where c has 0 and both a and b have 1: both must change, so it is 2.
  • Counting two flips where c has 1 and both are 0: one flip of either is enough.
  • Stopping when a and b reach 0: c may still have higher 1 bits that need flips; loop while any of the three is non-zero.
  • Already equal: the answer is 0, as with a = 1, b = 2, c = 3.

Summary

Walk the bits from the right. Where c has 1, add 1 if a and b are both 0; where c has 0, add the number of 1s in a and b. At most 32 iterations, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Read the lowest bits

    Take x = a & 1, y = b & 1, z = c & 1.

  2. Count this position

    If z is 1, add 1 when x and y are both 0. If z is 0, add x + y.

  3. Shift and repeat

    Shift a, b and c right by one until all three are 0, then return the count.

Frequently asked questions

Why can each bit be handled on its own?

OR works bit by bit: bit i of a OR b depends only on bit i of a and bit i of b. Flipping a bit in one position never changes another, so the minimum total is the sum of the minimum flips at each position.


Why does a 0 in c sometimes cost two flips?

a OR b has a 0 only when both a and b have 0 there. If both have 1, both must be flipped, which is 2. If only one has 1, it is 1. A 1 in c never costs more than 1, because flipping either a or b is enough.


Is there a one-line version?

Yes: popcount((a | b) ^ c) + popcount(a & b & ~c). The first term counts the positions where a OR b is wrong, at one flip each; the second adds the extra flip where c has 0 and both a and b have 1. The loop shows the same reasoning one bit at a time.

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description