please help me with this
Everything seems fine still not getting submitted?
Hi Vikash… u are unnecessarily making things complicated… all u have to do is to count no of set bits…refer to my soln…
Well I understood it, but I want to know why my code is not working here??
pls explain ur approach what u are trying to do…
As every number that can be expressed as power of 2 have a binary representation like this :
2 = 10
4 = 100
8 =1000
so i am finding the nearest 2 raised to power number
for example lets say n = 11
so 11 in binary is => 1011
so i am counting the position till the most significant bit
as in 1011 that position is => pos = 3 as there are 3 more bits after the most significant bit
and now to find the nearest number lets say it is ‘k’ and => k = pow(2,pos)
k=pow(2,3) = 8
ans =1+(n-k) => ans= 1+(11-8) = 4
so we need 4 steps or jumps to reach the enemy
u haven’t read the soln explanation given properly.
result = 1 + min steps for (n-k) remaining steps.
this is what is mentioned…
so result = 1 + min steps for (n-k)
ie result = 1 + min steps for(11-8)
ie result = 1 + min steps for(3)
ie result = 1 + 2 = 3(ans)
ohh okok now i got it thanks
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.