Solution 1
The Key Idea for Solving This Coding Question
If a number n
is a power of two, the follow statements hold.
- is true and is a non-negtive integer.
- There is exactly one
1
-bit in the binary representation of n
.
The key idea is to understand that for any number n
, doing a bit-wise AND of n
and n−1
flips the least-significant 1
-bit of n
to 0
.
C++ Code
Time Complexity
Space Complexity
Solution 2
The Key Idea for Solving This Coding Question
C++ Code
Time Complexity
Space Complexity
Solution 3
The Key Idea for Solving This Coding Question
Recursion
C++ Code
Time Complexity
Space Complexity
Miscellaneous