Advertisement
LikeRampage

C++/C#/Python leetcode 342. Power of Four chatgpt

Nov 12th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | Source Code | 0 0
  1. Certainly! The problem "Power of Four" on LeetCode asks you to determine if a given integer is a power of four. A number is a power of four if it can be expressed as
  2. 4
  3. n
  4. 4
  5. n
  6.   where
  7. n
  8. n is a non-negative integer.
  9.  
  10. Here's a simple C++ solution to solve this problem:
  11.  
  12. class Solution {
  13. public:
  14.    bool isPowerOfFour(int n) {
  15.        // Check if n is positive and if the bitwise representation of n has only one bit set
  16.        // and that bit is in an even position (which corresponds to powers of four)
  17.        return n > 0 && (n & (n - 1)) == 0 && (n & 0xAAAAAAAA) == 0;
  18.    }
  19. };
  20. Explanation:
  21. Check if n is positive: A power of four must be greater than zero.
  22. Check if n is a power of two: The expression (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set in their binary representation.
  23. Check if the bit is at an even position: The expression (n & 0xAAAAAAAA) == 0 ensures that the only set bit (if it exists) is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary representation 10101010101010101010101010101010, which has bits set at the positions of powers of four.
  24. Example Usage:
  25. You can test this function using the following main function:
  26.  
  27. #include <iostream>
  28.  
  29. int main() {
  30.    Solution solution;
  31.    int n;
  32.  
  33.    // Test with some examples
  34.    n = 16; // 4^2
  35.    std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
  36.  
  37.    n = 5;  // Not a power of four
  38.    std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
  39.  
  40.    n = 64; // 4^3
  41.    std::cout << "Is " << n << " a power of four? " << (solution.isPowerOfFour(n) ? "Yes" : "No") << std::endl;
  42.  
  43.    return 0;
  44. }
  45. Output:
  46. Is 16 a power of four? Yes
  47. Is 5 a power of four? No
  48. Is 64 a power of four? No
  49. This code correctly determines whether the given integer is a power of four.
  50.  
  51. write code C# leetcode 342. Power of Four
  52.  
  53. Certainly! Here’s a C# solution for the LeetCode problem 342, "Power of Four". The approach is similar to the C++ solution, where we check if the number is a power of four using bit manipulation.
  54.  
  55. C# Code:
  56. public class Solution {
  57.    public bool IsPowerOfFour(int n) {
  58.        // Check if n is positive, a power of two, and that the single bit is in an even position
  59.        return n > 0 && (n & (n - 1)) == 0 && (n & 0xAAAAAAAA) == 0;
  60.    }
  61. }
  62. Explanation:
  63. Positive Check: We first check if n is greater than 0, as powers of four are positive integers.
  64. Power of Two Check: The condition (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set.
  65. Even Position Check: The condition (n & 0xAAAAAAAA) == 0 ensures that the set bit is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary 10101010101010101010101010101010, which has bits set at the positions of powers of four.
  66. Example Usage:
  67. You can test this function in a console application:
  68.  
  69. using System;
  70.  
  71. public class Program {
  72.    public static void Main() {
  73.        Solution solution = new Solution();
  74.        
  75.        // Test with some examples
  76.        int[] testCases = { 16, 5, 64, 1, 0, -4 };
  77.        
  78.        foreach (int n in testCases) {
  79.            Console.WriteLine($"Is {n} a power of four? {solution.IsPowerOfFour(n)}");
  80.        }
  81.    }
  82. }
  83. Output:
  84. Is 16 a power of four? True
  85. Is 5 a power of four? False
  86. Is 64 a power of four? False
  87. Is 1 a power of four? True
  88. Is 0 a power of four? False
  89. Is -4 a power of four? False
  90. This code provides a method to determine if a given integer is a power of four and includes a test harness to demonstrate its functionality.
  91.  
  92. Certainly! Here’s a Python solution for the LeetCode problem 342, "Power of Four". The approach is similar to the previous solutions, where we check if the number is a power of four using bit manipulation.
  93.  
  94. Python Code:
  95. class Solution:
  96.    def isPowerOfFour(self, n: int) -> bool:
  97.        # Check if n is positive, a power of two, and that the single bit is in an even position
  98.        return n > 0 and (n & (n - 1)) == 0 and (n & 0xAAAAAAAA) == 0
  99.  
  100. Explanation:
  101. Positive Check: We first check if n is greater than 0, as powers of four are positive integers.
  102. Power of Two Check: The expression (n & (n - 1)) == 0 checks if n is a power of two. This works because powers of two have exactly one bit set in their binary representation.
  103. Even Position Check: The expression (n & 0xAAAAAAAA) == 0 ensures that the set bit is in an even position. The hexadecimal 0xAAAAAAAA corresponds to the binary representation 10101010101010101010101010101010, which has bits set at the positions of powers of four.
  104. Example Usage:
  105. You can test this function with the following example:
  106.  
  107. if __name__ == "__main__":
  108.    solution = Solution()
  109.    
  110.    # Test with some examples
  111.    test_cases = [16, 5, 64, 1, 0, -4]
  112.    
  113.    for n in test_cases:
  114.        print(f"Is {n} a power of four? {solution.isPowerOfFour(n)}")
  115. Output:
  116. Is 16 a power of four? True
  117. Is 5 a power of four? False
  118. Is 64 a power of four? False
  119. Is 1 a power of four? True
  120. Is 0 a power of four? False
  121. Is -4 a power of four? False
  122. This code defines a method to determine if a given integer is a power of four and includes a test harness to demonstrate its functionality.
Tags: C++ C# python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement