Minimum Flips to Make a OR b Equal to c
Count the fewest bit flips in a and b that make a OR b equal c, one bit position at a time.
Given three positive integers abcababa | bcab
Example 1
Output: 3
Explanation: after the flips a = 1 and b = 4, and 1 | 4 = 5.
Example 2
Output: 1
Explanation: flipping the lowest bit of a gives a = 5, and 5 | 2 = 7.
Example 3
Output: 0
Explanation: 1 | 2 is already 3.
The Idea
Picture a row of lamps, each wired to two switches, one in a top row and one in a bottom row. A lamp is lit when at least one of its two switches is on. You are told which lamps should be lit, and you want to get there by flipping as few switches as possible. That is this problem: the switches are the bits of aba | bc
To see the switches, write the numbers in binary. In Example 1, 2 is 010110101
The bitwise OR compares two numbers position by position, and its bit is 1 when at least one of the two bits is 1. So 2 | 6 is 110c101
Each column is one bit position. In the teal column, position 2, ab
At position 1, both abccaa = 001b = 100
Did the flips at one position change what another position needed? No. The OR at position 1 only looks at the two bits at position 1. So each position is a small problem of its own, and the answer is the sum of the flips each position needs. Those flips depend on just three bits: the one in cab
Solution Approach
Turning that into code means settling three things: why the positions can be handled one by one instead of trying sets of flips, how many flips one position needs, and how to read the bits one position at a time. There is also an optional shorter version that works on whole numbers at once.
Positions instead of sets of flips
Why not try different sets of flips until a | bcab
How many bits can abintab
Flips needed at one position
How many flips does one position need? It depends on the bit of cab
When the bit of caba | b
Reading one bit at a time
How do we get at a single bit? The bitwise AND, &a & 1b = 6110
To reach the next position, shift the number right by one with b >>= 1110011abc
When can the reading stop? Once abccab
Optional: counting with whole numbers
Can the bit-by-bit reading be skipped? Java's Integer.bitCount(x)x^(a | b) ^ ca | b~cca & b & ~cabc
int flips = Integer.bitCount((a | b) ^ c) + Integer.bitCount(a & b & ~c);
For Example 1, (a | b) ^ c110 ^ 101 = 011a & b & ~c010abc
Java Solution
The method minFlips
- First, set
, the running total, to 0.flips - Start a loop that runs while
,a orb is not 0. Inside, read the bit at position 0 of each number withc , into& 1 ,bitA andbitB .bitC - If
is 1, add 1 tobitC only whenflips is 0, that is, when both bits are 0.bitA | bitB - If
is 0, addbitC , one flip for each 1 that must be turned off.bitA + bitB - Shift
,a andb right by one withc , so the next position moves to position 0.>>= 1 - When the loop ends, return
.flips
The maina = b = 7c = 1a = Integer.MAX_VALUE
public class MinimumFlips {
public static int minFlips(int a, int b, int c) {
int flips = 0;
while (a != 0 || b != 0 || c != 0) {
int bitA = a & 1;
int bitB = b & 1;
int bitC = c & 1;
if (bitC == 1) {
if ((bitA | bitB) == 0) {
flips += 1; // turn on either bit
}
} else {
flips += bitA + bitB; // every 1 must become 0
}
a >>= 1;
b >>= 1;
c >>= 1;
}
return flips;
}
public static void main(String[] args) {
System.out.println(minFlips(2, 6, 5));
System.out.println(minFlips(4, 2, 7));
System.out.println(minFlips(1, 2, 3));
System.out.println(minFlips(7, 7, 1));
System.out.println(minFlips(8, 3, 5));
System.out.println(minFlips(Integer.MAX_VALUE, 1, 1));
}
}
Running it prints:
3
1
0
4
3
30
The 3, 1 and 0 at the top match Examples 1, 2 and 3. The 4 on the fourth line comes from a = b = 111c = 001abc
The fifth line, 3, compares numbers of different lengths: a = 81000b = 30011c = 50101ba
Look at the last line, 30. Integer.MAX_VALUEc = 1a
Complexity
The loop does a fixed amount of work per bit position, and the largest of the three numbers decides how many positions there are.
- Time: O(log max(a, b, c)), because each pass shifts all three numbers right by one position, and the loop ends once the largest of them has no 1 bits left, after at most 31 passes for a positive
.int - Space: O(1), because the method keeps only
and the three bits of the current position, and shifts its own copies offlips ,a andb .c