Power of Four - Complete Solution Guide
Power of Four is LeetCode problem 342, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 == 4 x . Example 1: Input: n = 16 Output: true Example 2: Input: n = 5 Output: false Example 3: Input: n = 1 Output: true Constraints: -2 31 <= n <= 2 31 - 1 Follow up: Could you solve it without loops/recursion?
Detailed Explanation
The problem asks to determine if a given integer `n` is a power of four. This means checking if there exists an integer `x` such that `n == 4^x`. The input is an integer `n` within the range of a 32-bit signed integer. The output is a boolean value: `true` if `n` is a power of four, and `false` otherwise. The problem specifically encourages finding a solution without using loops or recursion.
Solution Approach
The provided solutions employ different strategies. The Python and Java solutions use an iterative approach to repeatedly divide by 4 until either the result is 1 (indicating a power of four) or the remainder is non-zero (indicating it's not a power of four). The C++ solution cleverly uses bit manipulation: it checks if only one bit is set (meaning it's a power of two) and then verifies that it's a power of four by checking the pattern of the set bit's position. The C solution also uses bit manipulation, specifically checking if the number is a power of 2 and then examining the specific bit pattern characteristic of powers of four.
Step-by-Step Algorithm
- Step 1: Handle edge cases. Check if `n` is less than or equal to 0. If so, return `false` because powers of four are always positive.
- Step 2 (Iterative approach): Repeatedly divide `n` by 4 using integer division (`//` in Python, `/` in Java). If the remainder is ever non-zero, `n` is not a power of four, so return `false`. If after repeated division, `n` becomes 1, then it is a power of four, so return `true`.
- Step 2 (Bit manipulation approach): Check if `n` is a power of two using `(n & (n - 1)) == 0`. If not, return `false`. Then, check the specific bit pattern characteristic of powers of four (e.g., only one bit is set and its position has specific properties).
- Step 3: Return the result (true or false) based on the checks.
Key Insights
- Insight 1: Powers of four are also powers of two. This allows us to use bit manipulation techniques more efficiently. Only one bit will be set in the binary representation of a power of four.
- Insight 2: Bit manipulation techniques are significantly faster than iterative or recursive approaches, allowing for constant time complexity solutions. Analyzing the binary representation is crucial.
- Insight 3: Handling edge cases like negative numbers and zero is important. Powers of four are always positive.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Math, Bit Manipulation, Recursion.
Companies
Asked at: Qualcomm, Two Sigma, Wix.