Skip to content

Commit 88fa611

Browse files
authored
Create 0342-power-of-four.java
1 parent 56003f0 commit 88fa611

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Easy/0342-power-of-four.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)