Skip to content

Commit c360aac

Browse files
Update Day4_PowerOfFour.java
1 parent 2714782 commit c360aac

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

Leetcode/Leetcode_August_Challenge/Day4_PowerOfFour.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,25 @@ public static boolean isPowerOfFour_log(int num) {
4242
return Math.floor(logn) == Math.ceil(logn);
4343
}
4444

45-
//approach 3
46-
static boolean isPowerOfFour2(int num)
47-
{
48-
int count = 0;
49-
50-
/*Check if there is
51-
only one bit set in n*/
52-
int x = num & (num - 1);
53-
54-
if ( num > 0 && x == 0)
55-
{
56-
/* count 0 bits
57-
before set bit */
58-
while(num > 1)
59-
{
60-
num >>= 1;
61-
count += 1;
62-
}
63-
64-
/*If count is even
65-
then return true
66-
else false*/
67-
return (count % 2 == 0) ? true : false;
68-
}
69-
70-
/* If there are more than
71-
1 bit set then n is not a
72-
power of 4*/
73-
return false;
74-
}
45+
// approach 3
46+
static boolean isPowerOfFour2(int num) {
47+
int count = 0;
48+
49+
// Check if there is only one bit set in num
50+
int x = num & (num - 1);
51+
52+
if (num > 0 && x == 0) {
53+
// count 0 bits before set bit
54+
while (num > 1) {
55+
num >>= 1;
56+
count += 1;
57+
}
58+
59+
// If count is even then return true else false
60+
return (count % 2 == 0) ? true : false;
61+
}
62+
63+
// If there are more than 1 bit set then num is not a power of 4
64+
return false;
65+
}
7566
}

0 commit comments

Comments
 (0)