Given a sequence of three binary sequences A, B and C of N bits. Count the minimum bits required to flip in A and B such that XOR of A and B is equal to C. For Example :
Input: N = 3 A = 110 B = 101 C = 001 Output: 1 We only need to flip the bit of 2nd position of either A or B, such that A ^ B = C i.e., 100 ^ 101 = 001
A Naive approach is to generate all possible combination of bits in A and B and then XORing them to Check whether it is equal to C or not. Time complexity of this approach grows exponentially so it would not be better for large value of N.
An Efficient approach is to use concept of XOR.
XOR Truth Table Input Output X Y Z 0 0 - 0 0 1 - 1 1 0 - 1 1 1 - 0
If we generalize, we will find that at any position of A and B, we just only need to flip ith (0 to N-1) position of either A or B otherwise we will not able to achieve minimum no of Bits.
So at any position of i (0 to N-1) you will encounter two type of situation i.e., either A[i] == B[i] or A[i] != B[i]. Let’s discuss it one by one.
If A[i] == B[i] then XOR of these bits will be 0, two cases arise in C: C[i]==0 or C[i]==1.
If C[i] == 0, then no need to flip the bit but if C[i] == 1 then we have to flip the bit either in A[i] or B[i] so that 1^0 == 1 or 0^1 == 1.
If A[i] != B[i] then XOR of these Bits gives a 1, In C two cases again arise i.e., either C[i] == 0 or C[i] == 1.
Therefore if C[i] == 1, then we need not to flip the bit but if C[i] == 0, then we need to flip the bit either in A[i] or B[i] so that 0^0==0 or 1^1==0
Time Complexity: O(N)
Auxiliary space: O(1)
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.”
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- XOR of all elements of array with set bits equal to K
- Count pairs in an array such that both elements has equal set bits
- Minimum value of N such that xor from 1 to N is equal to K
- Choose X such that (A xor X) + (B xor X) is minimized
- Check if bits of a number has count of consecutive set bits in increasing order
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of even set bits between XOR of two arrays
- Count set bits in Bitwise XOR of all adjacent elements upto N
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Toggle bits of a number except first and last bits
- Print numbers having first and last bits as the only set bits
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Number of subarrays such that XOR of one half is equal to the other
- Count pairs of elements such that number of set bits in their AND is B[i]
- Check if all bits can be made same by flipping two consecutive bits
- Minimum bit flips such that every K consecutive bits contain at least one set bit
Improved By : nitin mittal