We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 56003f0 commit 88fa611Copy full SHA for 88fa611
Easy/0342-power-of-four.java
@@ -0,0 +1,26 @@
1
+/**
2
+ * 342. Power of Four
3
+ * Difficulty: Easy
4
+ * URL: https://leetcode.com/problems/power-of-four/
5
+ *
6
+ * ---------------------
7
+ * Approach: Iterative Division
8
9
+ * - If n <= 0, return false (negative numbers and zero can’t be powers of 4).
10
+ * - Repeatedly divide n by 4 while it is divisible by 4.
11
+ * - After the loop, if n reduces to 1 → it’s a power of 4, otherwise not.
12
13
+ * Time Complexity: O(log₄ n) // dividing by 4 each time
14
+ * Space Complexity: O(1)
15
+ */
16
+
17
18
+class Solution {
19
+ public boolean isPowerOfFour(int n) {
20
+ if (n <= 0)
21
+ return false;
22
+ while (n % 4 == 0)
23
+ n = n / 4;
24
+ return n == 1;
25
+ }
26
+}
0 commit comments