Check if Number is Power of Two in DSA Go - Time & Space Complexity
We want to know how fast we can check if a number is a power of two.
How does the time needed change as the number gets bigger?
Analyze the time complexity of the following code snippet.
func isPowerOfTwo(n int) bool {
if n <= 0 {
return false
}
return (n & (n - 1)) == 0
}
This code checks if a number is a power of two using a bitwise trick.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single bitwise AND operation.
- How many times: Exactly once, no loops or recursion.
Execution time stays almost the same no matter how big the number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation |
| 100 | 1 operation |
| 1000 | 1 operation |
Pattern observation: The time does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the check takes the same amount of time no matter how big the number is.
[X] Wrong: "Checking if a number is a power of two requires looping through all bits or dividing repeatedly."
[OK] Correct: The bitwise trick does the check in one step without loops, making it very fast.
Knowing this quick check shows you understand bitwise operations and efficient coding, a skill valued in many coding challenges.
"What if we used a loop to divide the number by two repeatedly until it is 1? How would the time complexity change?"
Practice
Solution
Step 1: Understand binary representation of powers of two
A power of two in binary has exactly one '1' bit and the rest are '0's. For example, 4 is 100 in binary.Step 2: Analyze the options
The statement "It has exactly one bit set to 1 in its binary form" matches the binary property of powers of two. The other options do not: powers of two (except 1) are even, not divisible by 3, and do not have all bits set to 1.Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option AQuick Check:
Power of two = one '1' bit [OK]
- Thinking powers of two are always odd
- Confusing powers of two with numbers divisible by 3
- Assuming all bits are set to 1
n is a power of two?Solution
Step 1: Understand bitwise check for power of two
The expressionn & (n - 1) == 0is true only ifnis a power of two (and n > 0).Step 2: Analyze each option
The snippetif n & (n - 1) == 0 { /* power of two */ }uses the correct bitwise AND operation. The others use incorrect bitwise operations that do not correctly check for power of two.Final Answer:
if n & (n - 1) == 0 { /* power of two */ } -> Option CQuick Check:
Use n & (n-1) == 0 to check power of two [OK]
- Using bitwise OR instead of AND
- Using n + 1 instead of n - 1
- Using XOR which does not work here
package main
import "fmt"
func main() {
n := 16
if n > 0 && (n & (n - 1)) == 0 {
fmt.Println("Power of Two")
} else {
fmt.Println("Not Power of Two")
}
}Solution
Step 1: Evaluate the condition for n = 16
16 in binary is 10000. 16 - 1 = 15 (binary 01111). Bitwise AND: 10000 & 01111 = 0.Step 2: Check the if condition
Since n > 0 and bitwise AND is 0, the condition is true, so it prints "Power of Two".Final Answer:
Power of Two -> Option BQuick Check:
16 & 15 == 0 means power of two [OK]
- Ignoring n > 0 check
- Misunderstanding bitwise AND result
- Assuming all even numbers are powers of two
n is a power of two. What is the error?
func isPowerOfTwo(n int) bool {
return n & (n - 1) == 0
}Solution
Step 1: Understand the function logic
The expressionn & (n - 1) == 0returns true for powers of two and also for zero.Step 2: Identify missing check
Zero and negative numbers are not powers of two, but this function returns true for zero. It lacks a check for n > 0.Final Answer:
It does not handle the case when n is zero or negative. -> Option DQuick Check:
Check n > 0 before bitwise test [OK]
- Not checking for n > 0
- Misusing bitwise operators
- Assuming parentheses cause error
true if a number n is a power of two, but also handles zero and negative inputs correctly. Which of the following implementations is correct?Solution
Step 1: Check for positive input
The function must ensurenis greater than zero to exclude zero and negatives.Step 2: Use correct bitwise condition
The condition(n & (n - 1)) == 0correctly identifies powers of two.Step 3: Analyze options
The implementationfunc isPowerOfTwo(n int) bool { return n > 0 && (n & (n - 1)) == 0 }correctly combines both checks. The others use incorrect operators or conditions.Final Answer:
func isPowerOfTwo(n int) bool { return n > 0 && (n & (n - 1)) == 0 } -> Option AQuick Check:
Check n > 0 and n & (n-1) == 0 [OK]
- Using bitwise OR instead of AND
- Not checking n > 0
- Using wrong bitwise expressions
Ready to prove it?
Pass the test and this pattern moves unknown
