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.
"The other post (above) explaining the logic" is exactly how I reached the logic. I usually prefer mathematical solutions because of better time-complexity.
While trying to find a mathematical pattern, I concluded that I required the count of "initial 1s and final 0s". Further, instead of counting 1s and 0s, I wanted to reach a unified solution. Thinking over that I realized "bits in N-1" would allow me do that, hence, the solution. For detailed explanation, please check the reply above.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counter game
You are viewing a single comment's thread. Return to all comments →
"The other post (above) explaining the logic" is exactly how I reached the logic. I usually prefer mathematical solutions because of better time-complexity.
While trying to find a mathematical pattern, I concluded that I required the count of "initial 1s and final 0s". Further, instead of counting 1s and 0s, I wanted to reach a unified solution. Thinking over that I realized "bits in N-1" would allow me do that, hence, the solution. For detailed explanation, please check the reply above.