Check if Number is Power of Two in DSA Typescript - Time & Space Complexity
We want to know how fast we can check if a number is a power of two. This helps us understand how the time needed grows as the number gets bigger.
The question is: How many steps does it take to decide if a number is a power of two?
Analyze the time complexity of the following code snippet.
function isPowerOfTwo(n: number): boolean {
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 a subtraction.
- How many times: Exactly once, no loops or recursion.
Since the code does just one operation regardless of input size, the time stays the same even if the number grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of steps does not increase 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, so it is much faster.
Knowing this quick check shows you understand how to use bitwise operations smartly, a skill that impresses in coding interviews.
What if we changed the input to be a string representing a binary number? How would the time complexity change?
Practice
Solution
Step 1: Understand the definition of power of two
A number is a power of two if it equals 2 raised to an integer exponent, like 2^0=1, 2^1=2, 2^2=4, etc.Step 2: Check other options
Odd numbers, divisibility by 3, or primality do not define powers of two.Final Answer:
It can be written as 2 raised to some integer exponent -> Option CQuick Check:
Power of two = 2^n [OK]
- Thinking all powers of two are odd
- Confusing powers of two with prime numbers
- Assuming divisibility by 3 means power of two
n is a power of two using bitwise operations?Solution
Step 1: Understand the bitwise check for power of two
The expressionn & (n - 1)clears the lowest set bit. For powers of two, this results in zero.Step 2: Analyze each option
return n > 0 && (n & (n - 1)) === 0; uses the correct bitwise AND with (n-1). Options A, B, and C use incorrect bitwise operators or operands.Final Answer:
return n > 0 && (n & (n - 1)) === 0; -> Option AQuick Check:
Bitwise AND with (n-1) = 0 means power of two [OK]
- Using bitwise OR instead of AND
- Using (n + 1) instead of (n - 1)
- Forgetting to check n > 0
function isPowerOfTwo(n: number): boolean {
return n > 0 && (n & (n - 1)) === 0;
}
console.log(isPowerOfTwo(16));
console.log(isPowerOfTwo(18));Solution
Step 1: Check isPowerOfTwo(16)
16 in binary is 10000. 16 & 15 (01111) = 0, so returns true.Step 2: Check isPowerOfTwo(18)
18 in binary is 10010. 18 & 17 (10001) = 16 (not zero), so returns false.Final Answer:
true false -> Option DQuick Check:
16 is power of two, 18 is not [OK]
- Assuming 18 is power of two because it's even
- Ignoring the n > 0 condition
- Misreading bitwise AND results
function checkPower(n: number): boolean {
return (n & (n - 1)) === 0;
}Solution
Step 1: Analyze the function logic
The function uses bitwise AND correctly but does not check if n is positive.Step 2: Understand why n > 0 is needed
Without n > 0, input 0 returns true incorrectly because 0 & -1 = 0.Final Answer:
Missing check for n > 0 to avoid false positives -> Option AQuick Check:
Check n > 0 before bitwise test [OK]
- Ignoring zero or negative inputs
- Assuming bitwise alone is enough
- Misunderstanding bitwise AND behavior
const numbers = [0, 1, 2, 3, 4, 5, 8, 16, 18]; const powersOfTwo = numbers.filter(n => ???); console.log(powersOfTwo);
Solution
Step 1: Understand the filter condition
We want to keep numbers that are powers of two, so the condition must check n > 0 and bitwise AND with (n-1) equals zero.Step 2: Evaluate each option
n > 0 && (n & (n - 1)) === 0 correctly uses the standard power of two check. Options B, C, and D use incorrect bitwise operations or conditions.Final Answer:
n > 0 && (n & (n - 1)) === 0 -> Option BQuick Check:
Filter with bitwise AND and n > 0 [OK]
- Using bitwise OR or XOR instead of AND
- Not checking n > 0
- Using (n + 1) instead of (n - 1)
Ready to prove it?
Pass the test and this pattern moves unknown
