Count minimum bits to flip such that XOR of A and B equal to C

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to count the Minimum bits in A and B
#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 both A[i] and B[i] are equal
        if (A[i] == B[i] && C[i] == '1')
            ++count;
  
        // If Both A and B are unequal
        else if (A[i] != B[i] && C[i] == '0')
            ++count;
    }
    return count;
}
  
//Driver Code
int main()
{
    //N represent total count of Bits
    int N = 5;
    char a[] = "10100";
    char b[] = "00010";
    char c[] = "10011";
  
    cout << totalFlips(a, b, c, N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to count the Minimum bits in A and B
class GFG {
      
    static int totalFlips(String A, String B,
                                String C, int N)
    {
        int count = 0;
          
        for (int i = 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
            else if (A.charAt(i) != B.charAt(i)
                          && C.charAt(i) == '0')
                ++count;
        }
          
        return count;
    }
      
    //driver code
    public static void main (String[] args)
    {
        //N represent total count of Bits
        int N = 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


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to find minimum bits to be flip
def totalFlips(A, B, C, N):
  
    count = 0
    for i in range(N):
          
        # If both A[i] and B[i] are equal
        if A[i] == B[i] and C[i] == '1':
            count=count+1
  
        # if A[i] and B[i] are unequal
        elif A[i] != B[i] and C[i] == '0':
            count=count+1
    return count
  
  
# Driver Code
# N represent total count of Bits
N = 5
a = "10100"
b = "00010"
c = "10011"
print(totalFlips(a, b, c, N))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to count the Minimum
// bits flip in A and B
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 both A[i] and B[i] are equal
            if (A[i] == B[i] && C[i] == '1')
                ++count;
  
            // If Both A and B are unequal
            else if (A[i] != B[i] && C[i] == '0')
                ++count;
        }
        return count;
    }
  
    // Driver code
    public static void Main()
    {
        // N represent total count of Bits
        int N = 5;
        string a = "10100";
        string b = "00010";
        string c = "10011";
  
        Console.Write(totalFlips(a, b, c, N));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to count the
// Minimum bits in A and B
  
function totalFlips($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
        else if ($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";
  
echo totalFlips($a, $b, $c, $N);
  
// This code is contributed by nitin mittal.
?>

chevron_right



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.”



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.