From 079598f2f5a81b4eae08042e25a4310f23fc9b69 Mon Sep 17 00:00:00 2001 From: SNishanth2202 Date: Sun, 19 Oct 2025 23:32:42 +0530 Subject: [PATCH] Added solution for problem 342 --- quizzes/_342.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 quizzes/_342.py diff --git a/quizzes/_342.py b/quizzes/_342.py new file mode 100644 index 0000000..a6b2053 --- /dev/null +++ b/quizzes/_342.py @@ -0,0 +1,16 @@ +""" + Question: Power of Four + Desc: 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. + URL: https://leetcode.com/problems/power-of-four/ + Resource: Runtime: 28 ms, Memory Usage: 16.5 MB +""" +class Solution: + def isPowerOfFour(self, n: int) -> bool: + # A number is power of four if: + # 1. It's greater than 0 + # 2. It's a power of two (n & (n-1)) == 0 + # 3. The only set bit is at an even position + return n > 0 and (n & (n - 1)) == 0 and (n - 1) % 3 == 0