A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

For a non negative integer in the range 0 = i = n write a C++ programming to calculate the number of 1's in their binary representation and return them as an array
Q:

For a non negative integer in the range 0 = i = n write a C++ programming to calculate the number of 1's in their binary representation and return them as an array

0

For a non negative integer in the range 0 = i = n write a C++ programming to calculate the number of 1's in their binary representation and return them as an array

Input: 9
Output: true
Input: 81
Output: true
Input: 45
Output: false

Sample Output:

Original number: 4
0 1 1 2 1 
Original number: 7
0 1 1 2 1 2 2 3

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#include <vector>
using namespace std;

vector countBits(int num) {
    	vector res;
        for (int i = 0; i <= num; ++i) {
            res.push_back(0);
        }

        for (int i = 1; i <= num; ++i) {
            res[i] = res[i / 2] + i % 2;
        }

        return res;
    }
    
int main()
{
    int n = 4;
    vector result;
	cout << "Original number: " << n << endl; 
	result = countBits(n);
	for (int x : result) 
        cout << x << " "; 
    n = 7;    
	cout << "\nOriginal number: " << n << endl; 
	result = countBits(n);
	for (int x : result) 
        cout << x << " ";   
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now