Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Counting Bits problem solution

YASH PAL, 31 July 2024

In this Leetcode Counting Bits problem solution we have given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1’s in the binary representation of i.

Leetcode Counting Bits problem solution

Problem solution in Python.

class Solution(object):
    def countBits(self, num):
        if num == 0: return [0]
        dp = [0]*(num+1)
        dp[0] = 0
        dp[1] = 1
        for i in range(2, num+1):
            if i%2 == 0: dp[i] = dp[i/2]
            else: dp[i] = 1 + dp[(i-1)/2]
                
        return dp

Problem solution in Java.

class Solution {
    public int[] countBits(int num) {
        int[] dp = new int[num+1];
        
        dp[0] = 0;
        int nearest = 0;
        for (int k = 1; k <= num; k++) {
            if ((k & (k-1)) == 0) {
                dp[k] = 1;
                nearest = k;
            } else {
                dp[k] = dp[k-nearest] + dp[nearest];
            }
        }
        
        return dp;
    }
}

Problem solution in C++.

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int>res(num+1);
        res[0] = 0;
        for(int i = 1 ; i <= num ; i++)
            res[i] = res[i/2] + (i&1);
        return res;
    }
};

Problem solution in C.

int* countBits(int num, int* returnSize) {
    int i =0,k = 4, ano = 4;
    int num1 = num;
    *returnSize = (num+1);
    int *arr = malloc(sizeof(int)*(num+1));
    if(num1>3)
        num1 = 3;
    switch(num1){
        case 3:
            arr[3] = 2;
            num--;
            num1--;
        case 2:
            arr[2] = 1;
            num--;
            num1--;
        case 1:
            arr[1] = 1;
            num--;
            num1--;
        case 0:
            arr[0] = 0;
            num--;
            break;
    }
    while(num >= 0){
        if(i>=ano) {
            ano *= 2;
            i = 0;
        }
        arr[k] = arr[i%ano] + 1;
        i++;
        k++;
        num--;
    }
    return arr;

}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes