Leetcode 342: Power of Four
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.
Problem
Approach
Steps
Complexity
Input: The input is an integer n.
Example: n = 64
Constraints:
• -2^31 <= n <= 2^31 - 1
Output: The output is a boolean value indicating whether n is a power of four.
Example: true
Constraints:
• The result is true if n is a power of four, otherwise false.
Goal: The goal is to determine if n is a power of four using efficient conditions.
Steps:
• Check if the number is greater than 0.
• Ensure that the number is a power of 2 by using the bitwise AND operation.
• Ensure that the number minus one is divisible by 3.
Goal: The solution must handle the integer n within the range [-2^31, 2^31 - 1].
Steps:
• The solution must not exceed O(1) time complexity.
Assumptions:
• The input integer n is within the valid range of 32-bit signed integers.
• Input: n = 64
• Explanation: 64 is a power of four, as 64 = 4^3.
• Input: n = 12
• Explanation: 12 is not a power of four, so the result is false.
• Input: n = 1
• Explanation: 1 is a power of four, as 4^0 = 1.
Approach: The approach to solving this problem involves using bitwise operations to check if the number is a power of four.
Observations:
• A number is a power of four if it is greater than 0 and if only one bit is set to 1 in its binary representation.
• We can use bitwise AND to check if a number is a power of two and verify divisibility by 3 for powers of four.
Steps:
• Ensure the number is greater than 0.
• Check if the number is a power of 2 using the bitwise condition: (num & (num - 1)) == 0.
• Check if (num - 1) is divisible by 3.
Empty Inputs:
• If n is less than or equal to zero, it cannot be a power of four.
Large Inputs:
• Large inputs within the valid range should be handled without overflow or errors.
Special Values:
• The edge case where n = 1 should return true as 4^0 = 1.
Constraints:
• The solution must run in constant time, O(1).
bool isPowerOfFour(int num) {
return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
}
1 : Function Declaration
bool isPowerOfFour(int num) {
This is the function declaration. It takes an integer `num` and returns a boolean value indicating whether `num` is a power of four.
2 : Return Statement
return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
The return statement checks three conditions: first, that `num` is positive, second, that `num` is a power of two (by using the bitwise AND operation), and third, that `num - 1` is divisible by 3, ensuring that it is a power of four.
Best Case: O(1)
Average Case: O(1)
Worst Case: O(1)
Description: The time complexity is constant because the solution involves only bitwise operations.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant because only a few variables are used.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus