aboutsummaryrefslogtreecommitdiff
path: root/power_of_four.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-15 08:33:19 +0200
committerJack Sangdahl <[email protected]>2025-08-15 08:33:19 +0200
commitf035e4f04939219d5979f67d9e36b307a60d15fb (patch)
treeedeb5fee7f6c229ee0b84c3cf1f7741ec6841f9e /power_of_four.c
parent3ae87fe80a3a60df750acefceff38c4ab09a9a30 (diff)
downloadleetcode-f035e4f04939219d5979f67d9e36b307a60d15fb.tar.gz
leetcode-f035e4f04939219d5979f67d9e36b307a60d15fb.zip
342. power of four
Diffstat (limited to 'power_of_four.c')
-rw-r--r--power_of_four.c14
1 files changed, 2 insertions, 12 deletions
diff --git a/power_of_four.c b/power_of_four.c
index 7ab3cae..47f88fe 100644
--- a/power_of_four.c
+++ b/power_of_four.c
@@ -1,6 +1,5 @@
// 342. Power of Four
-#include <stdbool.h>
-#include <stdio.h>
+#include "leetcode.h"
/*
* given an integer 'n', return 'true' if it is a power of four, otherwise,
@@ -9,16 +8,7 @@
*/
bool isPowerOfFour(int n) {
- int ans = 0, pos = 1, ones = 0;
- while (n) {
- if (n & 1) { // if 'n' is even...
- ans++;
- ones = pos;
- }
- n >>= 1; // 'n' / 2
- pos++;
- }
- return ans == 1 && ones & 1;
+ return n > 0 && __builtin_popcount((unsigned)n) == 1 && !((n - 1) % 3);
}
int main() {