Incredible Hulk

I don’t know where I am getting wrong.
Here is the code-link: https://ide.codingblocks.com/s/330959

Hey @nikhil015
Just explain me this:

{
p = 1<<(pos-1);
cout<<(n-p)+1;
}

What exactly u are trying to do here?

@Kartikkhariwal1
For eg. if n is 6 then while loop exit at the condition when p=8 and pos=3(set bit position). Since pos>n then the nearest power of 2 is 1<<(pos-1) which is 4 and then I subtract it from n, the no. of steps left plus 1(1 step take by hulk to the nearest power of 2 with respect to n).

Okay @nikhil015
But after taking largest step of 2s power smaller than n
How anser is n-p+1
For remaining n-p you have to follow the same

Final answer will be total set bits in N

@Kartikkhariwal1 Thanks, I think I missed that I also follow the same approach for the remaining n-p steps. Even I didn’t recognize in my own example. That quite very funny. But thanks for helping me out. I greatly appreciate your help.

1 Like