Check if Number is Power of Two in DSA Java - Time & Space Complexity
We want to know how fast the program can tell 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.
public boolean isPowerOfTwo(int 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.
Since the code does only one check regardless of input size, the time stays the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time does not increase as the number gets bigger.
Time Complexity: O(1)
This means the check takes the same short time no matter how big the number is.
[X] Wrong: "Checking if a number is a power of two needs to check all smaller numbers or divide repeatedly."
[OK] Correct: This code uses a quick bit trick that does the check in one step without loops or division.
Knowing this quick check shows you understand how bits work and can write fast code, a skill valued in many coding challenges.
"What if we changed the input to a long type instead of int? 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, e.g., 4 is 100.Step 2: Check other options
Odd numbers and divisibility by 3 do not define powers of two. Having all bits set to 1 is not a power of two.Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option CQuick Check:
Power of two = single 1 bit [OK]
- Thinking all odd numbers are powers of two
- Confusing powers of two with numbers having all bits set
- Assuming divisibility by 3 means power of two
n is a power of two?Solution
Step 1: Understand 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 other options
Options A, C, and D use incorrect bitwise operations that do not correctly check power of two.Final Answer:
return (n & (n - 1)) == 0; -> Option BQuick Check:
Use n & (n-1) == 0 to check power of two [OK]
- Using OR or XOR instead of AND in bitwise check
- Checking n & (n + 1) which is incorrect
- Not considering zero or negative numbers
int n = 16; boolean result = (n & (n - 1)) == 0; System.out.println(result);
Solution
Step 1: Calculate n & (n - 1) for n = 16
16 in binary is 10000, 15 is 01111. Bitwise AND is 00000 (0).Step 2: Compare result to zero
Since 16 & 15 == 0, the expression is true, so result is true.Final Answer:
true -> Option AQuick Check:
16 is power of two, so output is true [OK]
- Confusing bitwise AND with OR
- Expecting false for powers of two
- Ignoring parentheses in expression
public static boolean isPowerOfTwo(int n) {
return (n & n - 1) == 0;
}Solution
Step 1: Identify issue with negative numbers
For n = Integer.MIN_VALUE (-2147483648), which is 0x80000000 in two's complement, n-1 = 0x7FFFFFFF (Integer.MAX_VALUE), and 0x80000000 & 0x7FFFFFFF == 0, so it incorrectly returns true.Step 2: Rule out other options
Return type is correct, it uses AND (&), and operator precedence correctly parses as n & (n - 1) (no missing parentheses needed).Final Answer:
Method does not handle negative numbers. -> Option AQuick Check:
n > 0 && (n & (n-1)) == 0 [OK]
- Not checking n > 0 before bitwise operation
- Ignoring negative input handling
- Confusing bitwise AND with OR
true if a given positive integer n is a power of two, and false otherwise. Which of the following implementations correctly handles all positive integers including 1, and excludes zero and negative numbers?Solution
Step 1: Check for positive number condition
Zero and negative numbers are not powers of two, son > 0is required.Step 2: Use bitwise check for power of two
The expression(n & (n - 1)) == 0correctly identifies powers of two including 1.Step 3: Analyze other options
return (n & (n - 1)) == 0; misses positive check, allowing zero. Options C and D use incorrect bitwise operations.Final Answer:
return n > 0 && (n & (n - 1)) == 0; -> Option DQuick Check:
Check positive and n & (n-1) == 0 [OK]
- Not excluding zero and negative numbers
- Using incorrect bitwise operators
- Missing parentheses around (n - 1)
Ready to prove it?
Pass the test and this pattern moves unknown
