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 problem can be solved in much simpler way. There's no requirement of several fancy functions, "power", "log" etc., just bit-manipulation does the trick. Unfortunately, even "editorial" didn't give best/simplest of the solution. Let me share my approach for guys looking for better solution.
First of all, there's no need to "actually perform the operations on N". The game just requires counting set-bits in binary representation of N-1. This can be accomplished in complexity, where b=set-bits in (N-1). Here's code in C :
Counter game
You are viewing a single comment's thread. Return to all comments →
The problem can be solved in much simpler way. There's no requirement of several fancy functions, "power", "log" etc., just bit-manipulation does the trick. Unfortunately, even "editorial" didn't give best/simplest of the solution. Let me share my approach for guys looking for better solution.
First of all, there's no need to "actually perform the operations on N". The game just requires counting set-bits in binary representation of N-1. This can be accomplished in complexity, where b=set-bits in (N-1). Here's code in C :
Obviously, language like Python gives even shorter solution. Hope this simplifies the problem to some extent.
Any comments/suggestions for further improvement are welcomed :)
Informative Tweets for Inquisitive Minds