Absolute difference between set and unset bit count in N
Last Updated :
23 Apr, 2023
Prerequisite: Bitset function in STL library
Given a number N, the task is to find the absolute difference of the number of set and unset bits of this given number.
Examples:
Input: N = 14
Output: 2
Explanation:
Binary representation of 14 is “1110”.
Here the number of set bits is 3 and the number of unset bits is 1.
Therefore, the absolute difference is 2.
Input: N = 56
Output: 0
Explanation:
Binary representation of 56 is “110100”.
Here the number of set bits is 3 and the number of unset bits is 3.
Therefore, the absolute difference 0.
Approach:
- Count the total number of bits in the binary representation of the given number.
- Use bitset function defined in the STL library, to count the number of set bits efficiently.
- Then, we will subtract the set bits from the total number of bits to get the number of unset bits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int sz = 64;
int totalbits( int N)
{
return ( int )(1 + log2(N));
}
int absoluteDifference( int N)
{
bitset<sz> arr(N);
int total_bits = totalbits(N);
int set_bits = arr.count();
int unset_bits = total_bits
- set_bits;
int ans = abs (set_bits
- unset_bits);
return ans;
}
int main()
{
int N = 14;
cout << absoluteDifference(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int sz = 64 ;
static int totalbits( int N)
{
return ( 1 + ( int )(Math.log(N) /
Math.log( 2 )));
}
static int absoluteDifference( int N)
{
int arr = N;
int total_bits = totalbits(N);
int set_bits = countSetBits(arr);
int unset_bits = total_bits - set_bits;
int ans = Math.abs(set_bits - unset_bits);
return ans;
}
static int countSetBits( int n)
{
int count = 0 ;
while (n > 0 )
{
n &= (n - 1 );
count++;
}
return count;
}
public static void main(String[] args)
{
int N = 14 ;
System.out.println(absoluteDifference(N));
}
}
|
Python3
import math
sz = 64
def totalbits(N) :
return ( 1 + ( int )(math.log(N) / math.log( 2 )))
def absoluteDifference(N) :
arr = N
total_bits = totalbits(N)
set_bits = countSetBits(arr)
unset_bits = total_bits - set_bits
ans = abs (set_bits - unset_bits)
return ans
def countSetBits(n) :
count = 0
while (n > 0 ) :
n = n & (n - 1 )
count + = 1
return count
N = 14
print (absoluteDifference(N))
|
C#
using System;
class GFG{
static int totalbits( int N)
{
return (1 + ( int )(Math.Log(N) /
Math.Log(2)));
}
static int absoluteDifference( int N)
{
int arr = N;
int total_bits = totalbits(N);
int set_bits = countSetBits(arr);
int unset_bits = total_bits - set_bits;
int ans = Math.Abs(set_bits - unset_bits);
return ans;
}
static int countSetBits( int n)
{
int count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
static void Main() {
int N = 14;
Console.WriteLine(absoluteDifference(N));
}
}
|
Javascript
<script>
function totalbits(N)
{
return (1 + parseInt(Math.log(N) / Math.log(2), 10));
}
function absoluteDifference(N)
{
let arr = N;
let total_bits = totalbits(N);
let set_bits = countSetBits(arr);
let unset_bits = total_bits - set_bits;
let ans = Math.abs(set_bits - unset_bits);
return ans;
}
function countSetBits(n)
{
let count = 0;
while (n > 0)
{
n &= (n - 1);
count++;
}
return count;
}
let N = 14;
document.write(absoluteDifference(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1) as constant space for variables and bitset arr is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...