*You are given a number that might be so big that it doesn’t fit in any standard type, however the number is provided in a form of [a, b, c, d, …] where every letter stands for the bit number that is set in the byte representation, so you can read it easily. In example [1, 3, 4] represents 1101 = 13. Then you multiply the number by 3. How many bits are set to 1 in the binary representation of the result?*

This is a real interview question that has been asked to a friend of mine quite recently. To figure out the solution, we need to firstly consider what the multiplication by 3 really means. Let’s take an example.

01101

x00011

--------

01101

+11010

--------

0100111

As you can see the result is the sum of the number itself and the number shifted to left by 1. The former is obvious, as it is the result of multiplying by 1. The latter is what multiplication by 2 does. **Multiplication by 2^n shifts the byte left by n positions in a general case**.

Coming back to our result we get [1, 3, 4] x 3 = [1, 3, 4] << 1 + [1, 3, 4] = [2, 4, 5] + [1, 3, 4]. Now we may answer the question: what bytes will be set after this addition? Recalling the binary arithmetic and we may notice that the solution lays in calculating the result of the addition of the. In simpler words we just need to implement binary addition of numbers represented in the form of [2,4,5] and [1,3,4]. The answer will be the size of the result structure.

int getNumberOfSetBits(vector<int> number)
{
// calculate the number shifted by 2
vector<int> shifted = number;
for(unsigned int i = 0; i < number.size(); i++)
shifted[i]++;
bool carry = false;
vector<int> result;
// the first element of number will surely be a part of the result
result.push_back(number[0]);
// start the addition
for(unsigned int i = 1; i < number.size(); i++)
{
if(number[i] == shifted[i-1])
{
if(carry)
{
result.push_back(number[i]);
}
carry = true;
}
else
{
result.push_back(number[i]);
result.push_back(shifted[i-1]);
carry = false;
}
}
// do not forget the carry the last one from shifted
int last = shifted.size() - 1;
if(carry)
result.push_back(shifted[last] + 1);
else result.push_back(shifted[last]);
return result.size();
}

The code could be optimized as actually the structure result is not necessary. But it keeps more visible what was the idea behind the algorithm.

Regards

Joe Kidd

### Like this:

Like Loading...