We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
please note that "&" not a logical operator in python. It's a bit-wise operator.
How on earth did you come up with this logic!!!
a demo of how it works (it took me a while to figure it out so I'm sharing it):
say , let's take 5. so, binary(5) = 101 which our n here.
for the first loop iteration:
n = 101 & 1010 ( 101<<1)
= 0000
ans = 1
2nd time n>0 doesn't hold so output is 1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Consecutive 1's in Binary Numbers
You are viewing a single comment's thread. Return to all comments →
a demo of how it works (it took me a while to figure it out so I'm sharing it): say , let's take 5. so, binary(5) = 101 which our n here. for the first loop iteration: n = 101 & 1010 ( 101<<1) = 0000 ans = 1 2nd time n>0 doesn't hold so output is 1