loading...

### re: Algorithms Problem Solving: Reduce to zero VIEW POST

If you try writing that number in binary format and count the number of bits to make it zero would be the answer and the time complexity would be the length of binary string

Sounds like a similar problem on leetcode: leetcode.com/problems/counting-bits/

I don't see how this helps though, am I missing something?

8 requires 4 bits (log2(8) + 1) and has an expected answer of 4, there are 4 digits in 8: a one and 3 zeroes

14 has an expected answer of 6, but it's also 4 bits (log2(16) + 1), 3 ones and a zero.

Hey imagine this solution as travelling a series if bits where travelling has a cost, for example travelling from 0 to 1 and 0 to 0 will cost you "1" and travelling from 1 to 1 will cost you "2" and travelling from 1 to 0 will also cost you "2" and if the last bit is 1 then add "1"
So 10110 will cost you 7.

Ah ok, very cool! Thanks for explaining!

Can you please explain what you mean by count the number of bits to make it zero? I like the idea but I can't figure out how to go from 1110 to 6 or 1000 to 4 like in the example.

Code of Conduct Report abuse