Flips to Make OR Equal
You may flip single bits (a 0 to a 1 or a 1 to a 0) in the binary forms of a and b, as many times as you like and in either number. Return the smallest number of flips after which a | b (bitwise OR) equals c exactly. All three numbers are 31 bit non-negative integers.
Example 1
a 2
b 6
c 5
returns 3
Bit by bit: bit 0 of c is 1 but neither a nor b has it (1 flip); bit 1 of c is 0 but both have it (2 flips); bit 2 is already fine.
Example 2
a 4
b 2
c 7
returns 1
4 | 2 is 6, so only bit 0 is missing: set it in either number.
Constraints
0 <= a < 2**310 <= b < 2**310 <= c < 2**31
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Insert a Bit FieldMedium
- Longest Pair With No Shared LetterMedium
- Next Gray CodeMedium
- Subsets With a Target XORMedium
- The One Not TripledMedium
- Two Unpaired NumbersMedium
- Undo the XOR ChainMedium
- Well-Formed Byte StreamMedium