# Calculate the number of set bits for every number from 0 to N

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 __builtin_popcount(), find the number of set bits in all the required integers.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count ` `// of set bits in all the ` `// integers from 0 to n ` `void` `findSetBits(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i <= n; i++) ` ` ` `cout << __builtin_popcount(i) << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `findSetBits(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

0 1 1 2 1 2

**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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count ` `// of set bits in all the ` `// integers from 0 to n ` `void` `findSetBits(` `int` `n) ` `{ ` ` ` ` ` `// dp[i] will store the count ` ` ` `// of set bits in i ` ` ` `int` `dp[n + 1]; ` ` ` ` ` `// Initialise the dp array ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// Count of set bits in 0 is 0 ` ` ` `cout << dp[0] << ` `" "` `; ` ` ` ` ` `// For every number starting from 1 ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// If current number is even ` ` ` `if` `(i % 2 == 0) { ` ` ` ` ` `// Count of set bits in i is equal to ` ` ` `// the count of set bits in (i / 2) ` ` ` `dp[i] = dp[i / 2]; ` ` ` `} ` ` ` ` ` `// If current element is odd ` ` ` `else` `{ ` ` ` ` ` `// Count of set bits in i is equal to ` ` ` `// the count of set bits in (i / 2) + 1 ` ` ` `dp[i] = dp[i / 2] + 1; ` ` ` `} ` ` ` ` ` `// Print the count of set bits in i ` ` ` `cout << dp[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `findSetBits(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the count of set bits ` `# in all the integers from 0 to n ` `def` `findSetBits(n) : ` ` ` ` ` `# dp[i] will store the count ` ` ` `# of set bits in i ` ` ` `# Initialise the dp array ` ` ` `dp ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `); ` ` ` ` ` `# Count of set bits in 0 is 0 ` ` ` `print` `(dp[` `0` `], end ` `=` `" "` `); ` ` ` ` ` `# For every number starting from 1 ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` ` ` `# If current number is even ` ` ` `if` `(i ` `%` `2` `=` `=` `0` `) : ` ` ` ` ` `# Count of set bits in i is equal to ` ` ` `# the count of set bits in (i / 2) ` ` ` `dp[i] ` `=` `dp[i ` `/` `/` `2` `]; ` ` ` ` ` `# If current element is odd ` ` ` `else` `: ` ` ` ` ` `# Count of set bits in i is equal to ` ` ` `# the count of set bits in (i / 2) + 1 ` ` ` `dp[i] ` `=` `dp[i ` `/` `/` `2` `] ` `+` `1` `; ` ` ` ` ` `# Print the count of set bits in i ` ` ` `print` `(dp[i], end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` ` ` `findSetBits(n); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

0 1 1 2 1 2

## Recommended Posts:

- Program to calculate the number of odd days in given number of years
- Maximum number of contiguous array elements with same number of set bits
- Check if a number has same number of set and unset bits
- Calculate MDAS Factorial of given number
- Java Program to Calculate Power of a Number
- Find minimum number of Log value needed to calculate Log upto N
- Change all even bits in a number to 0
- Find a number containing N - 1 set bits at even positions from the right
- Count Set-bits of number using Recursion
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Count number of set bits in a range using bitset
- Program to invert bits of a number Efficiently
- Prime Number of Set Bits in Binary Representation | Set 1
- Count pairs of elements such that number of set bits in their OR is B[i]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.