Check if Number is Power of Two in DSA Python - Time & Space Complexity
We want to know how fast we can check if a number is a power of two.
How does the time to check grow as the number gets bigger?
Analyze the time complexity of the following code snippet.
def is_power_of_two(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 and subtraction.
- How many times: Exactly once, no loops or recursion.
The number of steps stays the same no matter how big the number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work does not increase as the number grows.
Time Complexity: O(1)
This means the check takes the same amount of time no matter how large the number is.
[X] Wrong: "Checking if a number is a power of two requires looping through all smaller numbers."
[OK] Correct: This bitwise method does the check instantly without loops, so it is much faster.
Knowing this quick check shows you understand efficient bitwise operations, a useful skill in many coding problems.
"What if we changed the input to allow zero and negative numbers? How would the time complexity change?"
Practice
Solution
Step 1: Understand powers of two
Powers of two are numbers like 1, 2, 4, 8, 16, 32, etc., where each is 2 raised to an integer.Step 2: Check each option
16 = 2^4, so it is a power of two. Others like 18, 20, 22 are not powers of two.Final Answer:
16 -> Option AQuick Check:
16 = 2^4 [OK]
- Confusing even numbers with powers of two
- Thinking any multiple of 2 is a power of two
n is a power of two?Solution
Step 1: Understand bitwise AND check
For powers of two, binary form has only one '1'. n & (n-1) clears the lowest set bit. If result is 0, n is power of two.Step 2: Analyze options
n & (n - 1) == 0 uses correct bitwise AND check. Others use OR, XOR, or addition which do not correctly check power of two.Final Answer:
n & (n - 1) == 0 -> Option BQuick Check:
Bitwise AND check is standard for power of two [OK]
- Using OR or XOR instead of AND
- Forgetting to check n > 0 before this test
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
print(is_power_of_two(32))
print(is_power_of_two(45))Solution
Step 1: Check 32
32 is 2^5, so n > 0 and n & (n-1) == 0 is True.Step 2: Check 45
45 is not a power of two, so n & (n-1) != 0, returns False.Final Answer:
True\nFalse -> Option CQuick Check:
32 power of two = True, 45 not = False [OK]
- Ignoring n > 0 condition
- Assuming all even numbers are powers of two
def check_power(n):
if n & (n - 1) == 0:
return True
else:
return False
print(check_power(0))Solution
Step 1: Analyze bitwise check
n & (n-1) == 0 works only if n > 0. For n=0, it returns True incorrectly.Step 2: Identify missing condition
Code lacks n > 0 check, so 0 incorrectly returns True.Final Answer:
Missing check for n > 0 before bitwise operation -> Option AQuick Check:
Always check n > 0 before bitwise test [OK]
- Not handling zero or negative inputs
- Using wrong bitwise operators
nums = [0, 1, 2, 3, 4, 5, 8, 16, 18] A) result = [x for x in nums if x & (x - 1) == 0] B) result = [x for x in nums if x > 0 and x & (x - 1) == 0] C) result = [x for x in nums if x | (x - 1) == 0] D) result = [x for x in nums if x > 0 or x & (x - 1) == 0]
Solution
Step 1: Understand filtering condition
We must check x > 0 to avoid zero or negative numbers passing the test.Step 2: Analyze each option
result = [x for x in nums if x > 0 and x & (x - 1) == 0] correctly uses x > 0 and bitwise check. result = [x for x in nums if x & (x - 1) == 0] misses x > 0, so 0 passes incorrectly. Options C and D use wrong operators or logic.Final Answer:
result = [x for x in nums if x > 0 and x & (x - 1) == 0] -> Option DQuick Check:
Filter with x > 0 and bitwise AND check [OK]
- Not filtering out zero
- Using OR instead of AND in condition
- Using bitwise OR instead of AND
Ready to prove it?
Pass the test and this pattern moves unknown
