Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
Last Updated :
27 Dec, 2021
Given three positive integers X, Y and Z, the task is to count the minimum numbers of bits required to be flipped in X and Y such that X OR Y (X | Y) is equal to Z.
Examples:
Input : X = 5 Y = 8 Z = 6
Output : 3
Explanation :
Before After
Flipping Flipping
X :: 0101 0100
Y :: 1000 0010
------ -----
Z :: 0110 0110
So we need to flip 3 bits in order
to make (X | Y) = Z .
Input : X = 1 Y = 2 Z = 3
Output : 0
Approach :
In order to solve this problem, we need to observe that the need to flip a bit X or Y arises only for the following conditions:
- If the current bit in Z is set and the corresponding bit in both X and Y is not set, we need to set a bit in either X or in Y.
- If the current bit in Z is unset, we need to unset the corresponding bit in A and B, whichever is set. Unset both if both are set.
Thus, we need to traverse through the bits of X, Y, and Z and follow the above two steps to get the desired result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumFlips( int X, int Y, int Z)
{
int res = 0;
while (X > 0 || Y > 0 || Z > 0) {
if (((X & 1) || (Y & 1)) && (Z & 1)) {
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
continue ;
}
else if (!(X & 1) && !(Y & 1) && (Z & 1)) {
res++;
}
else if ((X & 1) || (Y & 1) == 1)
{
if ((X & 1) && (Y & 1) && !(Z & 1)) {
res += 2;
}
else if (((X & 1) || (Y & 1)) && !(Z & 1))
{
res++;
}
}
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
}
return res;
}
int main()
{
int X = 5, Y = 8, Z = 6;
cout << minimumFlips(X, Y, Z);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minimumFlips( int X, int Y, int Z)
{
int res = 0 ;
while (X > 0 || Y > 0 || Z > 0 )
{
if (((X % 2 == 1 ) ||
(Y % 2 == 1 )) &&
(Z % 2 == 1 ))
{
X = X >> 1 ;
Y = Y >> 1 ;
Z = Z >> 1 ;
continue ;
}
else if (!(X % 2 == 1 ) &&
!(Y % 2 == 1 ) &&
(Z % 2 == 1 ))
{
res++;
}
else if ((X % 2 == 1 ) || (Y % 2 == 1 ))
{
if ((X % 2 == 1 ) &&
(Y % 2 == 1 ) &&
!(Z % 2 == 1 ))
{
res += 2 ;
}
else if (((X % 2 == 1 ) ||
(Y % 2 == 1 )) &&
!(Z % 2 == 1 ))
{
res++;
}
}
X = X >> 1 ;
Y = Y >> 1 ;
Z = Z >> 1 ;
}
return res;
}
public static void main(String[] args)
{
int X = 5 , Y = 8 , Z = 6 ;
System.out.print(minimumFlips(X, Y, Z));
}
}
|
Python3
def minimumFlips(X, Y, Z):
res = 0
while (X > 0 or Y > 0 or
Z > 0 ):
if (((X & 1 ) or (Y & 1 )) and
(Z & 1 )):
X = X >> 1
Y = Y >> 1
Z = Z >> 1
continue
elif ( not (X & 1 ) and not (Y & 1 ) and
(Z & 1 )):
res + = 1
elif ((X & 1 ) or (Y & 1 ) = = 1 ):
if ((X & 1 ) and (Y & 1 ) and
not (Z & 1 )):
res + = 2
elif (((X & 1 ) or (Y & 1 )) and
not (Z & 1 )):
res + = 1
X = X >> 1
Y = Y >> 1
Z = Z >> 1
return res
if __name__ = = "__main__" :
X = 5
Y = 8
Z = 6
print (minimumFlips(X, Y, Z))
|
C#
using System;
class GFG{
static int minimumFlips( int X, int Y, int Z)
{
int res = 0;
while (X > 0 || Y > 0 || Z > 0)
{
if (((X % 2 == 1) ||
(Y % 2 == 1)) &&
(Z % 2 == 1))
{
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
continue ;
}
else if (!(X % 2 == 1) &&
!(Y % 2 == 1) &&
(Z % 2 == 1))
{
res++;
}
else if ((X % 2 == 1) || (Y % 2 == 1))
{
if ((X % 2 == 1) &&
(Y % 2 == 1) &&
!(Z % 2 == 1))
{
res += 2;
}
else if (((X % 2 == 1) ||
(Y % 2 == 1)) &&
!(Z % 2 == 1))
{
res++;
}
}
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
}
return res;
}
public static void Main()
{
int X = 5, Y = 8, Z = 6;
Console.Write(minimumFlips(X, Y, Z));
}
}
|
Javascript
<script>
function minimumFlips(X, Y, Z)
{
var res = 0;
while (X > 0 || Y > 0 || Z > 0) {
if (((X & 1) || (Y & 1)) && (Z & 1))
{
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
continue ;
}
else if ((X & 1)==0 && (Y & 1)==0 &&
(Z & 1))
{
res++;
}
else if ((X & 1) || (Y & 1) == 1)
{
if ((X & 1) && (Y & 1) && (Z & 1)==0)
{
res += 2;
}
else if (((X & 1) || (Y & 1)) &&
(Z & 1)==0)
{
res++;
}
}
X = X >> 1;
Y = Y >> 1;
Z = Z >> 1;
}
return res;
}
var X = 5, Y = 8, Z = 6;
document.write(minimumFlips(X, Y, Z));
</script>
|
Share your thoughts in the comments
Please Login to comment...