Hulk min steps problem

In the sample input /output
3,4,5 the outputs is 2 ,1 ,2
we have to findout the min steps to reach the above no in terms of power of 2

for 3 , 2 steps is need as 2^1 = 2
for 4, 2 steps is need as 2^1 = 2 min
for 5 , 3 steps is need as 2^2 = 4 min is need
so the output will 2,2,3 but the output is 2,1,2

In this questions…You just have to find the total number of set bits in the number.
3 in binary is 011 So O/p is 2
4 in binary is 100 So O/p is 1
5 in binary is 101 So O/p is 2

Say for 5
2^2=4 So you reach 2 in 1 step…Now you need one more step to reach 5 that is 2^0=1 …So total step is 2.

@pratyush63
Does i need to count 2^0 = 1 , which one steps from my understanding of question
Hulk always jump in 2’s power.
so say 5
2^0 = 1
2^1 = 2
2^2 = 4
so to get near to enemy hulk needs only three steps. I am thinking in that way?

You also need to count 2^0

So to reach 5…you first reach 4 in 1 step…and then 5 in another step.

okay means first me find out the max in power of 2 which is less or equal to N, let say N =14
so hulk can take 2^3=8, then left 6 then 2^2 = 4 then left 2, 2^1 = 2 so total will be 3 Right ?

Yes…the answer would simply be the number of set bits in its binary format.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.