Whats the error in my code? Sample test cases passed but wrong answer.
Question : Bitmasking / Incredible Hulk
the link of my code :
Whats the error in my code?
your logic is incorrect.
let us try to understand the probleml through an example.
βin a single jump, hulk can go at a distance of 2^i for any i.β
examples:
-
N = 5.
answer: it need 2 jumps, first jump of length 4 and second of length 1.
how to solve it?
what powers of 2 can sum to 5? ans is 2^2 + 2^0 = 4+1 =5.(binary of 5 is 101 i.e. 2 set bits = 2 moves) -
N= 22,
answer : 3 jumps
how?
2^4 + 2^2+2^1 = 16+4+2 = 22 (binary form = 10110 i.e. 3 set bits in binary form = 3 moves)
note that the no of 1βs(set bits) in binary form is quickest no of moves to reach N.
thanks