Check if Number is Power of Two in DSA Javascript - Time & Space Complexity
We want to understand how fast we can check if a number is a power of two.
How does the time taken change as the number gets bigger?
Analyze the time complexity of the following code snippet.
function isPowerOfTwo(n) {
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 operations stays the same no matter how big the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the check takes the same amount of time regardless of the number size.
[X] Wrong: "The time to check grows with the size of the number because we need to divide or loop through bits."
[OK] Correct: This method uses a single bitwise operation that works instantly, so no loops or divisions are needed.
Knowing this quick check shows you understand efficient bitwise operations, a useful skill in many coding problems.
"What if we changed the method to repeatedly divide the number by 2 until it is 1? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of power of two
A power of two is a number that can be expressed as 2 raised to some integer exponent, like 2^0=1, 2^1=2, 2^2=4, etc.Step 2: Compare options with the definition
Only It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). correctly describes this. Options A, C, and D do not match the definition.Final Answer:
It can be written as 2 raised to an integer exponent (e.g., 2^3 = 8). -> Option CQuick Check:
Power of two = 2^n [OK]
- Thinking all even numbers are powers of two
- Confusing divisibility by 3 with powers of two
- Assuming number ending with 2 is power of two
n is a power of two using bitwise operations?Solution
Step 1: Understand bitwise check for power of two
A number that is a power of two has only one bit set. The expressionn & (n - 1)clears the lowest set bit. If the result is zero, it means only one bit was set.Step 2: Analyze each option
n & (n - 1) === 0 uses the correct expression. Options B, C, and D use incorrect bitwise operators or operands.Final Answer:
n & (n - 1) === 0 -> Option AQuick Check:
Bitwise AND with n-1 equals zero means power of two [OK]
- Using OR or XOR instead of AND
- Using n + 1 instead of n - 1
- Not comparing result to zero
function isPowerOfTwo(n) {
return n > 0 && (n & (n - 1)) === 0;
}
console.log(isPowerOfTwo(16));
console.log(isPowerOfTwo(18));Solution
Step 1: Evaluate isPowerOfTwo(16)
16 in binary is 10000. 16 & 15 (01111) = 0, so returns true.Step 2: Evaluate isPowerOfTwo(18)
18 in binary is 10010. 18 & 17 (10001) = 16 (not zero), so returns false.Final Answer:
true\nfalse -> Option DQuick Check:
16 is power of two: true, 18 is not: false [OK]
- Assuming all even numbers return true
- Forgetting to check n > 0
- Misunderstanding bitwise AND result
function checkPower(n) {
return (n & n - 1) === 0;
}Solution
Step 1: Test with edge case n=0
0 & (0 - 1) = 0 & (-1) = 0 === 0 returns true, but 0 is not a power of two.Step 2: Analyze options
The function lacks n > 0 check. Not checking if n is positive identifies this. No OR used (B wrong), parentheses not required due to operator precedence (C wrong), subtraction is correct (D wrong).Final Answer:
Not checking if n is positive -> Option BQuick Check:
n > 0 && (n & (n-1)) === 0 [OK]
- Ignoring operator precedence
- Not checking if number is positive
- Using wrong bitwise operators
nums to keep only numbers that are powers of two. Which code snippet correctly does this in JavaScript?Solution
Step 1: Understand filtering condition for power of two
We need to keep numbers greater than zero wheren & (n - 1) === 0is true.Step 2: Evaluate each filter condition
nums.filter(n => n > 0 && (n & (n - 1)) === 0) correctly uses this condition. nums.filter(n => n % 2 === 0) filters even numbers, which is incorrect. Options C and D use wrong bitwise operations.Final Answer:
nums.filter(n => n > 0 && (n & (n - 1)) === 0) -> Option AQuick Check:
Filter with bitwise AND and n > 0 [OK]
- Filtering only even numbers
- Using wrong bitwise operators
- Not checking for positive numbers
Ready to prove it?
Pass the test and this pattern moves unknown
