Problem Walkthrough

Min Flips OR Equal LeetCode 1318 Guide

Process each of the 32 bit positions independently, extracting bits from a, b, and c to determine whether the current a|b bit matches the desired c bit and counting the minimum flips required to make (a OR b) equal c.

8 min read|

Min Flips OR Equal

LeetCode 1318

Problem Overview

LeetCode 1318 Minimum Flips to Make a OR b Equal to c asks you to find the minimum number of bit flips in a and b so that the bitwise OR of a and b equals c. A flip changes a single bit in either a or b from 0 to 1 or from 1 to 0. You are given three non-negative integers a, b, and c.

The naive approach would involve trying all possible combinations of flips, but that is exponential. The key insight is that bit positions in OR operations are entirely independent — what happens at bit position 0 has absolutely no effect on what happens at bit position 1. This lets us decompose the problem into 32 independent single-bit subproblems.

For each bit position, we read the bits of a, b, and c at that position and determine the minimum cost to make (a_bit OR b_bit) equal c_bit. We then sum these per-position costs for the final answer.

  • Given: three non-negative integers a, b, c
  • Goal: return the minimum number of bit flips in a or b so that (a OR b) == c
  • A flip toggles a single bit in a or b from 0 to 1 or from 1 to 0
  • Key insight: each of the 32 bit positions is independent — solve each separately
  • Total answer: sum of flip costs across all bit positions

Bit-by-Bit Analysis

For each bit position i (from 0 to 31), extract the corresponding bits: a_bit = (a >> i) & 1, b_bit = (b >> i) & 1, and c_bit = (c >> i) & 1. The current OR result at this position is a_bit | b_bit. Compare it to c_bit to decide how many flips are needed.

If a_bit | b_bit already equals c_bit, no flips are needed at this position — cost is 0. If they differ, we need to flip one or more bits. The exact cost depends on whether c_bit is 0 or 1, and on the current values of a_bit and b_bit. There are only four possible (a_bit, b_bit) input combinations, making exhaustive case analysis tractable.

In practice we loop while any of a, b, or c is greater than zero, extracting the lowest bit of each with the & 1 mask and then right-shifting all three by 1 each iteration. This processes all significant bits without unnecessarily iterating over leading zeros — though capping at 32 iterations is equivalent and equally correct.

💡

Each Bit Position Is Independent

Each bit position is fully independent: a flip at position i does not affect any other position. The OR operation at position i depends only on a_bit and b_bit at position i. This means you can process all 32 bit positions separately and simply sum the costs — no dynamic programming or backtracking is needed.

Two Cases

Case 1: c_bit is 1. The OR result must be 1. The OR of a_bit and b_bit is 1 whenever at least one of them is 1. If both a_bit and b_bit are already 0 (giving OR = 0), we need exactly one flip — we can flip either a_bit or b_bit to 1. If at least one is already 1 (OR = 1), cost is 0.

Case 2: c_bit is 0. The OR result must be 0. The OR of a_bit and b_bit is 0 only when both are 0. If a_bit is 1, it must flip to 0 (cost +1). If b_bit is 1, it must also flip to 0 (cost +1). The total cost at this position is a_bit + b_bit, which is 0, 1, or 2.

Together these two cases cover all possibilities. Notice that case 2 can cost 2 flips while case 1 costs at most 1 flip. The asymmetry arises because OR is a disjunctive operation: achieving a 1 requires only one cooperating bit, while achieving a 0 requires all contributing bits to be 0.

  1. 1Extract bits: a_bit = a & 1, b_bit = b & 1, c_bit = c & 1
  2. 2If c_bit == 1: add 1 to flips only if (a_bit | b_bit) == 0 (both are 0)
  3. 3If c_bit == 0: add a_bit + b_bit to flips (each 1 in a or b must be cleared)
  4. 4Right-shift a, b, and c each by 1 to advance to the next bit position
  5. 5Repeat until a == 0 and b == 0 and c == 0

Why Case 2 Can Cost 2

The subtle case that trips up many solvers is when c_bit is 0 but both a_bit and b_bit are 1. At first glance it may seem like one flip should suffice — after all, one flip changes the problem. But this reasoning is wrong: OR is a function of both inputs simultaneously.

When a_bit = 1 and b_bit = 1, the current OR is 1 | 1 = 1. We need OR = 0. If we flip only a_bit to 0, the OR becomes 0 | 1 = 1 — still wrong. If we flip only b_bit to 0, the OR becomes 1 | 0 = 1 — still wrong. Both bits must become 0 before the OR drops to 0. Hence the minimum cost at this position is 2.

This is fundamentally different from the c_bit = 1 case where flipping just one bit suffices because OR is satisfied by any single 1. The cost asymmetry — at most 1 for c=1, at most 2 for c=0 — is baked into the nature of the OR operation itself.

ℹ️

The 2-Flip Subtlety

When c_bit = 0 but a_bit = 1 and b_bit = 1, the cost is 2, not 1. OR of two 1s is 1, and flipping only one of them still leaves a 1 in the other — the OR stays 1. Both bits must be set to 0 before the OR can reach 0. This is the only case that costs 2 flips, and missing it leads to an answer that is too small.

Walk-Through Example

Let a = 2 (binary 010), b = 6 (binary 110), c = 5 (binary 101). We need (a OR b) = (010 OR 110) = 110 = 6 to become 101 = 5. The expected answer is 3.

Bit 0 (least significant): a_bit = 0, b_bit = 0, c_bit = 1. c=1 and OR=0 → need at least one 1 → cost 1. Bit 1: a_bit = 1, b_bit = 1, c_bit = 0. c=0 and both are 1 → must flip both → cost 2. Bit 2: a_bit = 0, b_bit = 1, c_bit = 1. c=1 and OR=1 → already correct → cost 0.

Total flips = 1 + 2 + 0 = 3. This matches the expected output. The two-flip cost at bit 1 is the critical contribution — both a and b had 1 at that position but c required 0, so both had to be cleared.

  1. 1a=2 (010), b=6 (110), c=5 (101), total_flips=0
  2. 2Bit 0: a_bit=0, b_bit=0, c_bit=1 → OR=0 ≠ 1 → flip cost 1 → total=1
  3. 3Bit 1: a_bit=1, b_bit=1, c_bit=0 → OR=1 ≠ 0 → both must flip → cost 2 → total=3
  4. 4Bit 2: a_bit=0, b_bit=1, c_bit=1 → OR=1 == 1 → no flip needed → total=3
  5. 5All bits exhausted → return 3

Code Walkthrough — Python and Java

Python solution: initialize flips = 0, then loop while a or b or c. Inside the loop, extract a_bit = a & 1, b_bit = b & 1, c_bit = c & 1. If c_bit == 1 and (a_bit | b_bit) == 0, add 1 to flips. If c_bit == 0, add a_bit + b_bit to flips. Then right-shift a >>= 1, b >>= 1, c >>= 1. Return flips. Time O(32) = O(1), Space O(1).

Java solution follows identically: int flips = 0; while (a > 0 || b > 0 || c > 0) { int aBit = a & 1, bBit = b & 1, cBit = c & 1; if (cBit == 1) { if ((aBit | bBit) == 0) flips++; } else { flips += aBit + bBit; } a >>= 1; b >>= 1; c >>= 1; } return flips;

Both solutions run in constant time and constant space since integers have a fixed number of bits (32 in Java, effectively 32 for LeetCode constraints in Python). The loop executes at most 32 iterations regardless of input values. No auxiliary data structures are needed — only the three loop variables and the running flip count.

⚠️

Loop Condition Must Include a, b, and c

The while loop condition must be (a > 0 or b > 0 or c > 0), not just (c > 0). If a or b has set bits at positions where c is already 0, those bits still need to be counted as required flips. Stopping when c reaches 0 would miss all remaining 1-bits in a and b that must be cleared to satisfy the OR constraint.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now