SMHTE - Editorial

PROBLEM LINK:

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None.

EXPLANATION

This problem can be solved by Brute Force.
Dividing the number by 2 in a loop and checking for remainder would suffice.
But the trick is with smaller source code.A common BIT-TRICK of using this expression ((n)&(n-1)) would be handy,
if one wants to shorten the source code.

For n=0, ((n)&(n-1)) gives 0, so we need to handle it separately, isn’t it? (perhaps by using (!n && (n&(n-1))), or maybe, n!=(n&(n-1)) None of the test files, except the sample test cases has this special case. That’s why this code got accepted -> http://www.codechef.com/viewsolution/3328518 (And there are many others, I am sure.)

3 Likes

I think you meant “dividing” the number by 2.

1 Like