Skip to content

Minimum Flips to Make a OR b Equal to c

MediumBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

You have three positive integers a, b and c. A flip changes one bit of a or b (0 to 1 or 1 to 0). Return the minimum number of flips so that a | b == c (bitwise OR).

a = 2, b = 6, c = 5  →  3    (a = 010, b = 110, c = 101)
a = 4, b = 2, c = 7  →  1
a = 1, b = 2, c = 3  →  0

Constraints

  • 1 <= a <= 10^9
  • 1 <= b <= 10^9
  • 1 <= c <= 10^9