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

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
```

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