Minimum Flips to Make a OR b Equal to c - Problem

Given three positive numbers a, b, and c, return the minimum number of bit flips required to make (a OR b) == c.

A flip operation consists of changing any single bit from 1 to 0 or from 0 to 1 in the binary representation of a number.

The bitwise OR operation returns 1 if at least one of the corresponding bits is 1, otherwise 0.

Input & Output

Example 1 — Basic Case
$ Input: a = 2, b = 7, c = 5
Output: 2
💡 Note: Binary: a=010, b=111, c=101. Current a|b=111, target=101. Need to flip bits 1 in both a and b to make them 0.
Example 2 — No Flips Needed
$ Input: a = 4, b = 2, c = 6
Output: 0
💡 Note: Binary: a=100, b=010, c=110. Current a|b=110 equals target c=110, so no flips needed.
Example 3 — Need More 1s
$ Input: a = 1, b = 2, c = 7
Output: 1
💡 Note: Binary: a=001, b=010, c=111. Current a|b=011, need 111. Must flip bit 2 in either a or b to 1.

Constraints

  • 1 ≤ a, b, c ≤ 109

Visualization

Tap to expand
Minimum Flips to Make a OR b Equal to cInputa = 2 (010)b = 7 (111)c = 5 (101)ProcessCurrent: a|b = 111Target: c = 101Bit 1 mismatch:Output2 flipsMinimum flipsrequiredKey: For each bit position, apply OR logic rules• If target bit = 1: need at least one input bit = 1• If target bit = 0: both input bits must = 0
Understanding the Visualization
1
Input
Three integers a, b, c with their binary representations
2
Process
Check each bit position and count required flips
3
Output
Minimum number of bit flips needed
Key Takeaway
🎯 Key Insight: Check each bit position independently and apply OR operation rules to count minimum flips
Asked in
Facebook 15 Microsoft 12 Amazon 8
28.0K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen