Given three positives integers A, B, and C, the task is to count the minimum number of flipping of bits required in A and B, such that the Bitwise OR of A and B is equal to C or not.
Examples:
Input: A = 2, B = 2, C = 3
Output: 1
Explanation:
The binary representation of A is 010, B is 010 and C is 011.
Flip the 3rd bit of either A or B, such that A | B = C, i.e. 011 | 010 = 011.
Therefore, the total number of flips required is 1.
Input: A = 2, B = 6, C = 5
Output: 3
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say res, that stores the minimum number of the flipped bits required.
- Iterate over each bit of A, B, and C and perform the following steps:
- If the ith bit of C is not set, then check for the following:
- If the ith bit of A is set, increment res with 1, ith bit of A needs to be flipped.
- If the ith bit of B is set, increment res with 1, ith bit of B needs to be flipped.
- If the ith bit of C is set, then check for the following:
- If the ith bit of both A and B are not set, increment res with 1, either of the bit needs to be flipped.
- If the ith bit of both A and B are set, we do not have to flip any of the bits.
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumFlips( int A, int B, int C)
{
int res = 0;
for ( int i = 0; i < 32; i++) {
int x = 0, y = 0, z = 0;
if (A & (1 << i)) {
x = 1;
}
if (B & (1 << i)) {
y = 1;
}
if (C & (1 << i)) {
z = 1;
}
if (z == 0) {
if (x) {
res++;
}
if (y) {
res++;
}
}
if (z == 1) {
if (x == 0 && y == 0) {
res++;
}
}
}
return res;
}
int main()
{
int A = 2, B = 6, C = 5;
cout << minimumFlips(A, B, C);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int minimumFlips( int A, int B, int C)
{
int res = 0 ;
for ( int i = 0 ; i < 32 ; i++)
{
int x = 0 , y = 0 , z = 0 ;
if ((A & ( 1 << i)) != 0 )
{
x = 1 ;
}
if ((B & ( 1 << i)) != 0 )
{
y = 1 ;
}
if ((C & ( 1 << i)) != 0 )
{
z = 1 ;
}
if (z == 0 )
{
if (x == 1 )
{
res++;
}
if (y == 1 )
{
res++;
}
}
if (z == 1 )
{
if (x == 0 && y == 0 )
{
res++;
}
}
}
return res;
}
public static void main(String[] args)
{
int A = 2 , B = 6 , C = 5 ;
System.out.println(minimumFlips(A, B, C));
}
}
|
Python3
def minimumFlips(A, B, C):
res = 0
for i in range ( 32 ):
x, y, z = 0 , 0 , 0
if (A & ( 1 << i)):
x = 1
if (B & ( 1 << i)):
y = 1
if (C & ( 1 << i)):
z = 1
if (z = = 0 ):
if (x):
res + = 1
if (y):
res + = 1
if (z = = 1 ):
if (x = = 0 and y = = 0 ):
res + = 1
return res
if __name__ = = '__main__' :
A, B, C = 2 , 6 , 5
print (minimumFlips(A, B, C))
|
C#
using System;
class GFG {
static int minimumFlips( int A, int B, int C)
{
int res = 0;
for ( int i = 0; i < 32; i++) {
int x = 0, y = 0, z = 0;
if ((A & (1 << i)) != 0) {
x = 1;
}
if ((B & (1 << i)) != 0) {
y = 1;
}
if ((C & (1 << i)) != 0) {
z = 1;
}
if (z == 0) {
if (x == 1) {
res++;
}
if (y == 1) {
res++;
}
}
if (z == 1) {
if (x == 0 && y == 0) {
res++;
}
}
}
return res;
}
public static void Main( string [] args)
{
int A = 2, B = 6, C = 5;
Console.WriteLine(minimumFlips(A, B, C));
}
}
|
Javascript
<script>
function minimumFlips(A , B , C) {
var res = 0;
for (i = 0; i < 32; i++) {
var x = 0, y = 0, z = 0;
if ((A & (1 << i)) != 0) {
x = 1;
}
if ((B & (1 << i)) != 0) {
y = 1;
}
if ((C & (1 << i)) != 0) {
z = 1;
}
if (z == 0) {
if (x == 1) {
res++;
}
if (y == 1) {
res++;
}
}
if (z == 1) {
if (x == 0 && y == 0) {
res++;
}
}
}
return res;
}
var A = 2, B = 6, C = 5;
document.write(minimumFlips(A, B, C));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 May, 2021
Like Article
Save Article