Minimum Flips to Make a OR b Equal to c

MediumBit Manipulationbit manipulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given three non-negative integers a, b and c. A flip changes one bit (0 to 1 or 1 to 0) in the binary form of a or b. Return the minimum number of flips needed so that a OR b (bitwise OR) equals c.

Examples

Input: a = 2, b = 6, c = 5
Output: 3
`a = 010`, `b = 110`, `c = 101`: bit 0 needs one flip, bit 2 is fine, and bit 1 needs both `a` and `b` cleared.
Input: a = 4, b = 2, c = 7
Output: 1
`a = 100`, `b = 010`, `c = 111`: set bit 0 in either number.
Input: a = 1, b = 2, c = 3
Output: 0
`1 OR 2` is already 3.

Constraints

  • 0 <= a, b, c <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bit manipulation problems