Count minimum bits to flip such that XOR of A and B equal to C
Last Updated :
12 Sep, 2023
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.
Another 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
C++
#include<bits/stdc++.h>
using namespace std;
int totalFlips( char *A, char *B, char *C, int N)
{
int count = 0;
for ( int i=0; i < N; ++i)
{
if (A[i] == B[i] && C[i] == '1' )
++count;
else if (A[i] != B[i] && C[i] == '0' )
++count;
}
return count;
}
int main()
{
int N = 5;
char a[] = "10100" ;
char b[] = "00010" ;
char c[] = "10011" ;
cout << totalFlips(a, b, c, N);
return 0;
}
|
Java
class GFG {
static int totalFlips(String A, String B,
String C, int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; ++i)
{
if (A.charAt(i) == B.charAt(i) &&
C.charAt(i) == '1' )
++count;
else if (A.charAt(i) != B.charAt(i)
&& C.charAt(i) == '0' )
++count;
}
return count;
}
public static void main (String[] args)
{
int N = 5 ;
String a = "10100" ;
String b = "00010" ;
String c = "10011" ;
System.out.print(totalFlips(a, b, c, N));
}
}
|
Python3
def totalFlips(A, B, C, N):
count = 0
for i in range (N):
if A[i] = = B[i] and C[i] = = '1' :
count = count + 1
else if A[i] ! = B[i] and C[i] = = '0' :
count = count + 1
return count
N = 5
a = "10100"
b = "00010"
c = "10011"
print (totalFlips(a, b, c, N))
|
C#
using System;
class GFG {
static int totalFlips( string A, string B,
string C, int N)
{
int count = 0;
for ( int i = 0; i < N; ++i) {
if (A[i] == B[i] && C[i] == '1' )
++count;
else if (A[i] != B[i] && C[i] == '0' )
++count;
}
return count;
}
public static void Main()
{
int N = 5;
string a = "10100" ;
string b = "00010" ;
string c = "10011" ;
Console.Write(totalFlips(a, b, c, N));
}
}
|
PHP
<?php
function totalFlips( $A , $B , $C , $N )
{
$count = 0;
for ( $i = 0; $i < $N ; ++ $i )
{
if ( $A [ $i ] == $B [ $i ] &&
$C [ $i ] == '1' )
++ $count ;
else if ( $A [ $i ] != $B [ $i ] &&
$C [ $i ] == '0' )
++ $count ;
}
return $count ;
}
$N = 5;
$a = "10100" ;
$b = "00010" ;
$c = "10011" ;
echo totalFlips( $a , $b , $c , $N );
?>
|
Javascript
<script>
function totalFlips(A, B, C, N)
{
let count = 0;
for (let i = 0; i < N; ++i) {
if (A[i] == B[i] && C[i] == '1' )
++count;
else if (A[i] != B[i] && C[i] == '0' )
++count;
}
return count;
}
let N = 5;
let a = "10100" ;
let b = "00010" ;
let c = "10011" ;
document.write(totalFlips(a, b, c, N));
</script>
|
Time Complexity: O(N)
Auxiliary space: O(1)
Efficient Approach:
This approach follows O(log N) time complexity.
C++
#include <bits/stdc++.h>
using namespace std;
int totalFlips(string A, string B, string C, int N)
{
int INTSIZE = 31;
int ans = 0;
int i = 0;
while (N > 0) {
int a = stoi(A.substr(i * INTSIZE, min(INTSIZE, N)),
0, 2);
int b = stoi(B.substr(i * INTSIZE, min(INTSIZE, N)),
0, 2);
int c = stoi(C.substr(i * INTSIZE, min(INTSIZE, N)),
0, 2);
int Z = a ^ b ^ c;
ans += __builtin_popcount(Z);
i++;
N -= 32;
}
return ans;
}
int main()
{
int N = 5;
char a[] = "10100" ;
char b[] = "00010" ;
char c[] = "10011" ;
cout << totalFlips(a, b, c, N);
return 0;
}
|
Java
class GFG {
static int totalFlips(String A, String B, String C,
int N)
{
int INTSIZE = 31 ;
int ans = 0 ;
int i = 0 ;
while (N > 0 ) {
int a = Integer.parseInt(
A.substring(i * INTSIZE,
i * INTSIZE
+ Math.min(INTSIZE, N)),
2 );
int b = Integer.parseInt(
B.substring(i * INTSIZE,
i * INTSIZE
+ Math.min(INTSIZE, N)),
2 );
int c = Integer.parseInt(
C.substring(i * INTSIZE,
i * INTSIZE
+ Math.min(INTSIZE, N)),
2 );
int Z = a ^ b ^ c;
ans += Integer.bitCount(Z);
i++;
N -= 32 ;
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
String a = "10100" ;
String b = "00010" ;
String c = "10011" ;
System.out.print(totalFlips(a, b, c, N));
}
}
|
Python3
def totalFlips(A, B, C, N):
INTSIZE = 31
ans = 0
i = 0
while N > 0 :
a = int (A[i * INTSIZE: min (INTSIZE + i * INTSIZE, N)], 2 )
b = int (B[i * INTSIZE: min (INTSIZE + i * INTSIZE, N)], 2 )
c = int (C[i * INTSIZE: min (INTSIZE + i * INTSIZE, N)], 2 )
Z = a ^ b ^ c
ans + = bin (Z).count( '1' )
i + = 1
N - = 32
return ans
if __name__ = = '__main__' :
N = 5
a = "10100"
b = "00010"
c = "10011"
print (totalFlips(a, b, c, N))
|
C#
using System;
class Program {
static int TotalFlips( string A, string B, string C,
int N)
{
int INTSIZE = 31;
int ans = 0;
int i = 0;
while (N > 0) {
int a = Convert.ToInt32(
A.Substring(i * INTSIZE,
Math.Min(INTSIZE, N)),
2);
int b = Convert.ToInt32(
B.Substring(i * INTSIZE,
Math.Min(INTSIZE, N)),
2);
int c = Convert.ToInt32(
C.Substring(i * INTSIZE,
Math.Min(INTSIZE, N)),
2);
int Z = a ^ b ^ c;
ans += BitCount(Z);
i++;
N -= 32;
}
return ans;
}
static int BitCount( int i)
{
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101)
>> 24;
}
static void Main( string [] args)
{
int N = 5;
string a = "10100" ;
string b = "00010" ;
string c = "10011" ;
Console.WriteLine(TotalFlips(a, b, c, N));
}
}
|
Javascript
function TotalFlips(A, B, C, N)
{
let INTSIZE = 31;
let ans = 0;
let i = 0;
while (N > 0)
{
let a = parseInt(A.substring(i * INTSIZE, Math.min(INTSIZE + i * INTSIZE, N)), 2);
let b = parseInt(B.substring(i * INTSIZE, Math.min(INTSIZE + i * INTSIZE, N)), 2);
let c = parseInt(C.substring(i * INTSIZE, Math.min(INTSIZE + i * INTSIZE, N)), 2);
let Z = a ^ b ^ c;
ans += Z.toString(2).split( '1' ).length - 1;
i++;
N -= 32;
}
return ans;
}
let N = 5;
let a = "10100" ;
let b = "00010" ;
let c = "10011" ;
console.log(TotalFlips(a, b, c, N));
|
Why this code works?
We observe that bit must be flipped if A[i]^B[i] !=C[i]. So, we can get the number of flips by calculating the number of set bits in a^b^c where a,b,c are integer representations of binary string. But string length may be greater than 32, size of a typical int type. So, the plan is to divide the string into substrings of length 31 ,perform operations and count set bits as mentioned for each substring.
Time Complexity: O(log N) as the while loop runs for log31N times and counting set bits account for at most O(32) for 32-bit and O(64) for 64-bit and for each substring operation O(31).
Space Complexity: O(1) , to be noted that substring operation need O(32) space.
“
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...