Flip bits of the sum of count of set bits of two given numbers
Given two numbers A and B, the task is to count the number of set bits in A and B and flip the bits of the obtained sum.
Examples:
Input: A = 5, B = 7
Output: 2
Explanation:
Binary representation of A is 101.
Binary representation of B is 111.
Count of set bits in A and B = 2 + 3 = 5.
Binary representation of the sum obtained = 101
Flipping the bits of the sum, the number obtained is (010)2 = 2.
Therefore, the required output is 2.
Input: A = 76, B = 35
Output: 1
Explanation:
Binary representation of A is 1001100
Binary representation of B is 100011
Count of set bits in A and B = 3 + 3 = 6
Binary representation of the sum obtained = 110
Flipping the bits of the sum, the number obtained is (001)2 = 1.
Therefore, the required output is 1.
Naive Approach: The idea to solve this problem is to first traverse through the binary representation of both the numbers and count number of set bits in both the numbers. Finally, add them and invert the bits of the resultant number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSetBits( int n)
{
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int invertBits( int n)
{
int x = log2(n);
int m = 1 << x;
m = m | m - 1;
n = n ^ m;
return n;
}
void invertSum( int A, int B)
{
int temp = countSetBits(A)
+ countSetBits(B);
cout << invertBits(temp) << endl;
}
int main()
{
int A = 5;
int B = 7;
invertSum(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countSetBits( int n)
{
int count = 0 ;
while (n != 0 )
{
n &= (n - 1 );
count++;
}
return count;
}
static int invertBits( int n)
{
int x = ( int )(Math.log(n) / Math.log( 2 ));
int m = 1 << x;
m = m | m - 1 ;
n = n ^ m;
return n;
}
static void invertSum( int A, int B)
{
int temp = countSetBits(A) +
countSetBits(B);
System.out.print(invertBits(temp));
}
static public void main(String args[])
{
int A = 5 ;
int B = 7 ;
invertSum(A, B);
}
}
|
Python3
import math
def countSetBits(n):
count = 0
while (n ! = 0 ):
n & = (n - 1 )
count + = 1
return count
def invertBits(n):
x = ( int )(math.log(n) / math.log( 2 ))
m = 1 << x
m = m | m - 1
n = n ^ m
return n
def invertSum(A, B):
temp = countSetBits(A) + countSetBits(B)
print (invertBits(temp))
A = 5
B = 7
invertSum(A, B)
|
C#
using System;
class GFG{
static int countSetBits( int n)
{
int count = 0;
while (n != 0)
{
n &= (n - 1);
count++;
}
return count;
}
static int invertBits( int n)
{
int x = ( int )Math.Log(n, 2);
int m = 1 << x;
m = m | m - 1;
n = n ^ m;
return n;
}
static void invertSum( int A, int B)
{
int temp = countSetBits(A) +
countSetBits(B);
Console.WriteLine(invertBits(temp));
}
static void Main()
{
int A = 5;
int B = 7;
invertSum(A, B);
}
}
|
Javascript
<script>
function countSetBits(n)
{
var count = 0;
while (n != 0)
{
n &= (n - 1);
count++;
}
return count;
}
function invertBits(n)
{
var x = parseInt((Math.log(n) /
Math.log(2)));
var m = 1 << x;
m = m | m - 1;
n = n ^ m;
return n;
}
function invertSum(A, B)
{
var temp = countSetBits(A) +
countSetBits(B);
document.write(invertBits(temp));
}
var A = 5;
var B = 7;
invertSum(A, B);
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Last Updated :
25 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...