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
InputOutput
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++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ code to count the Minimum bits in A and B

#include<bits/stdc++.h>

usingnamespacestd;

inttotalFlips(char*A, char*B, char*C, intN)

{

intcount = 0;

for(inti=0; i < N; ++i)

{

// If both A[i] and B[i] are equal

if(A[i] == B[i] && C[i] == '1')

++count;

// If Both A and B are unequal

elseif(A[i] != B[i] && C[i] == '0')

++count;

}

returncount;

}

//Driver Code

intmain()

{

//N represent total count of Bits

intN = 5;

chara[] = "10100";

charb[] = "00010";

charc[] = "10011";

cout << totalFlips(a, b, c, N);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java code to count the Minimum bits in A and B

classGFG {

staticinttotalFlips(String A, String B,

String C, intN)

{

intcount = 0;

for(inti = 0; i < N; ++i)

{

// If both A[i] and B[i] are equal

if(A.charAt(i) == B.charAt(i) &&

C.charAt(i) == '1')

++count;

// If Both A and B are unequal

elseif(A.charAt(i) != B.charAt(i)

&& C.charAt(i) == '0')

++count;

}

returncount;

}

//driver code

publicstaticvoidmain (String[] args)

{

//N represent total count of Bits

intN = 5;

String a = "10100";

String b = "00010";

String c = "10011";

System.out.print(totalFlips(a, b, c, N));

}

}

// This code is contributed by Anant Agarwal.

chevron_right

filter_none

Python

filter_none

editclose

play_arrow

linkbrightness_4code

# Python code to find minimum bits to be flip

deftotalFlips(A, B, C, N):

count =0

fori inrange(N):

# If both A[i] and B[i] are equal

ifA[i] ==B[i] andC[i] =='1':

count=count+1

# if A[i] and B[i] are unequal

elifA[i] !=B[i] andC[i] =='0':

count=count+1

returncount

# Driver Code

# N represent total count of Bits

N =5

a ="10100"

b ="00010"

c ="10011"

print(totalFlips(a, b, c, N))

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# code to count the Minimum

// bits flip in A and B

usingSystem;

classGFG {

staticinttotalFlips(stringA, stringB,

stringC, intN)

{

intcount = 0;

for(inti = 0; i < N; ++i) {

// If both A[i] and B[i] are equal

if(A[i] == B[i] && C[i] == '1')

++count;

// If Both A and B are unequal

elseif(A[i] != B[i] && C[i] == '0')

++count;

}

returncount;

}

// Driver code

publicstaticvoidMain()

{

// N represent total count of Bits

intN = 5;

stringa = "10100";

stringb = "00010";

stringc = "10011";

Console.Write(totalFlips(a, b, c, N));

}

}

// This code is contributed by Anant Agarwal.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP code to count the

// Minimum bits in A and B

functiontotalFlips($A, $B, $C, $N)

{

$count= 0;

for($i= 0; $i< $N; ++$i)

{

// If both A[i] and

// B[i] are equal

if($A[$i] == $B[$i] &&

$C[$i] == '1')

++$count;

// If Both A and

// B are unequal

elseif($A[$i] != $B[$i] &&

$C[$i] == '0')

++$count;

}

return$count;

}

// Driver Code

// N represent total count of Bits

$N= 5;

$a= "10100";

$b= "00010";

$c= "10011";

echototalFlips($a, $b, $c, $N);

// This code is contributed by nitin mittal.

?>

chevron_right

filter_none

Output:

2

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 contribute@geeksforgeeks.org. 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.”