Calculate the number of set bits for every number from 0 to N
Last Updated :
08 Mar, 2022
Given a non-negative integer N, the task is to find the count of set bits for every number from 0 to N.
Examples:
Input: N = 3
Output: 0 1 1 2
0, 1, 2 and 3 can be written in binary as 0, 1, 10 and 11.
The number of 1’s in their binary representation are 0, 1, 1 and 2.
Input: N = 5
Output: 0 1 1 2 1 2
Naive approach: Run a loop from 0 to N and using inbuilt bit count function __builtin_popcount(), find the number of set bits in all the required integers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSetBits( int n)
{
for ( int i = 0; i <= n; i++)
cout << __builtin_popcount(i) << " " ;
}
int main()
{
int n = 5;
findSetBits(n);
return 0;
}
|
Java
class GFG
{
static void findSetBits( int n)
{
for ( int i = 0 ; i <= n; i++)
System.out.print(Integer.bitCount(i) + " " );
}
public static void main(String[] args)
{
int n = 5 ;
findSetBits(n);
}
}
|
Python 3
def count(n):
count = 0
while (n):
count + = n & 1
n >> = 1
return count
def findSetBits(n):
for i in range (n + 1 ):
print (count(i), end = " " )
if __name__ = = '__main__' :
n = 5
findSetBits(n)
|
C#
using System;
class GFG
{
static int count( int n)
{
int count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
static void findSetBits( int n)
{
for ( int i = 0; i <= n; i++)
Console.Write(count(i)+ " " );
}
public static void Main(String []args)
{
int n = 5;
findSetBits(n);
}
}
|
Javascript
<script>
function count(n)
{
let count = 0;
while (n > 0)
{
count += n & 1;
n >>= 1;
}
return count;
}
function findSetBits(n)
{
for (let i = 0; i <= n; i++)
document.write(count(i)+ " " );
}
let n = 5;
findSetBits(n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient approach: Let us write the binary representation of numbers in the range (0, 6).
0 in binary – 000
1 in binary – 001
2 in binary – 010
3 in binary – 011
4 in binary – 100
5 in binary – 101
6 in binary – 110
Since, any even number can be written as (2 * i) and any odd number can be written as (2 * i + 1) where i is a natural number.
2, 4 and 3, 6 have equal number of 1’s in their binary representation as multiplying any number is equivalent to shifting it left by 1 (read here).
Similarly, any even number 2 * i and i will have equal number of 1’s in its binary representation.
Number of 1’s in 5(101) is equal to number of 1’s in 2’s binary representation + 1. So in case of any odd number (2 * i + 1), it will be (number of 1’s in the binary representation of i) + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSetBits( int n)
{
int dp[n + 1];
memset (dp, 0, sizeof (dp));
cout << dp[0] << " " ;
for ( int i = 1; i <= n; i++) {
if (i % 2 == 0) {
dp[i] = dp[i / 2];
}
else {
dp[i] = dp[i / 2] + 1;
}
cout << dp[i] << " " ;
}
}
int main()
{
int n = 5;
findSetBits(n);
return 0;
}
|
Java
class GFG
{
static void findSetBits( int n)
{
int []dp = new int [n + 1 ];
System.out.print(dp[ 0 ] + " " );
for ( int i = 1 ; i <= n; i++)
{
if (i % 2 == 0 )
{
dp[i] = dp[i / 2 ];
}
else
{
dp[i] = dp[i / 2 ] + 1 ;
}
System.out.print(dp[i] + " " );
}
}
public static void main(String []args)
{
int n = 5 ;
findSetBits(n);
}
}
|
Python3
def findSetBits(n) :
dp = [ 0 ] * (n + 1 );
print (dp[ 0 ], end = " " );
for i in range ( 1 , n + 1 ) :
if (i % 2 = = 0 ) :
dp[i] = dp[i / / 2 ];
else :
dp[i] = dp[i / / 2 ] + 1 ;
print (dp[i], end = " " );
if __name__ = = "__main__" :
n = 5 ;
findSetBits(n);
|
C#
using System;
class GFG
{
static void findSetBits( int n)
{
int []dp = new int [n + 1];
Console.Write(dp[0] + " " );
for ( int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
dp[i] = dp[i / 2];
}
else
{
dp[i] = dp[i / 2] + 1;
}
Console.Write(dp[i] + " " );
}
}
public static void Main(String []args)
{
int n = 5;
findSetBits(n);
}
}
|
Javascript
<script>
function findSetBits(n)
{
let dp = new Array(n + 1);
dp.fill(0);
document.write(dp[0] + " " );
for (let i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
dp[i] = dp[parseInt(i / 2, 10)];
}
else
{
dp[i] = dp[parseInt(i / 2, 10)] + 1;
}
document.write(dp[i] + " " );
}
}
let n = 5;
findSetBits(n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...