Minimum Flips A OR B

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start•← → to step•? all shortcuts

Solution Guide: Minimum Flips A OR B

Approach

Process each bit position of a, b, and c from least significant to most significant. If c's bit is 0, both a and b bits must be 0, so count flips for any 1s. If c's bit is 1, at least one of a or b must be 1, so add a flip only if both are 0.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Bit Manipulation

Why It Works

Analyzing each bit independently reduces the problem to per-bit logic: a target 0-bit requires clearing both inputs (up to 2 flips), while a target 1-bit requires at most one flip if neither input contributes.

Updated Mar 2026