Counting Bits

Counting the Number of 1’s in Binary Representations of Numbers in a Range

In the world of programming, understanding binary numbers and efficiently processing them is a critical skill.

A common problem involves counting how many 1 bits are present in the binary representation of numbers within a given range.In this article, we will explore a solution to count the number of 1 bits in every number from 0 to n.

Pair with given sum - Two Sum

Problem Overview

You are given an integer n. Your task is to count the number of 1 bits (also known as set bits) in the binary representation of each number in the range [0, n].

The goal is to return an array output where output[i] represents the number of 1 bits in the binary representation of i.

Input

  • An integer n, where 0 <= n <= 1000.

Output

  • An array of integers, where each element output[i] represents the number of 1 bits in the binary representation of i.

We need to count the number of 1 bits in the binary representations of numbers from 0 to 4. Let’s convert these numbers to binary:

  • 0 in binary: 0 → Number of 1 bits: 0
  • 1 in binary: 1 → Number of 1 bits: 1
  • 2 in binary: 10 → Number of 1 bits: 1
  • 3 in binary: 11 → Number of 1 bits: 2
  • 4 in binary: 100 → Number of 1 bits: 1

This is the binary representation of the integer 2147483645:

Binary: 01111111111111111111111111111101
Number of 1 bits: There are 30 ones in this binary number.

Approach to Solving the Problem

To solve this problem efficiently, let’s explore a few strategies:

  1. Brute Force Approach:

    • For each number i in the range [0, n], convert it to binary and count the number of 1 bits. This can be done by converting the number to its binary representation (e.g., using Python’s bin() function) and counting the number of 1 bits (using str.count('1')).
    • This approach, while straightforward, is not the most optimized.
  2. Optimized Approach (Using Dynamic Programming):

    • Instead of converting each number to binary individually, we can use dynamic programming to compute the result for each number based on the previous number.
    • The key insight is that the number of 1 bits in a number i is related to the number of 1 bits in i // 2 (i.e., the number obtained by right-shifting i by 1 bit). Specifically:
      • If i is even, then i // 2 has the same number of 1 bits as i, and we just append 0 to its binary representation.
      • If i is odd, then i // 2 has one less 1 bit, but adding 1 to i restores the count.

There are mainly 4 approach to solve this problem – 

  1. Bit Manipulation – I
  2. Bit Manipulation – II
  3. In-Built Function
  4. Bit Manipulation (DP)

1. Bit Mask – I

Time & Space Complexity
  • Time complexity: O(1)
  • Space complexity: O(1)

2. Bit Mask – II

Time & Space Complexity
    • Time complexity: O(1)
    • Space complexity: O(1)

Code

3. Bit Mask (Optimal)

  • Time complexity: O(1)
  • Space complexity: O(1)

Code

4. Built-In Function

Time & Space Complexity

  • Time complexity: O(1)
  • Space complexity: O(1)

Code

More Articles