#342
Easy Algorithms Power of four
Math Bit Manipulation Recursion
51.8% acceptance
Jan 12, 2026
4462
422
Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n == 4x.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn is_power_of_four(n: i32) -> bool {
n > 0 && (n & (n - 1)) == 0 && (n & 0x55555555) != 0
}
}