15% off all plans & roadmaps Use code
See plansLeetCode 342 · Solution & editorial
Given an integer n, return true if it is a power of four. Otherwise, return false.
Power of Four is a Bit Manipulation Patterns problem, in the Bitwise Operations - Power of Two/Four Check subpattern. The pattern covers XOR tricks, counting bits, power-of-two checks, and Hamming weight
Check if number is power of two or four using bitwise operations.
The reference solution runs in O(1) time and O(1) space.
Power of Four has been reported in interviews at Two Sigma.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Power of Four is a Bit Manipulation Patterns problem, in the Bitwise Operations - Power of Two/Four Check subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.