aboutsummaryrefslogtreecommitdiff
path: root/reordered_power_2.c
diff options
context:
space:
mode:
Diffstat (limited to 'reordered_power_2.c')
-rw-r--r--reordered_power_2.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/reordered_power_2.c b/reordered_power_2.c
new file mode 100644
index 0000000..bdc818a
--- /dev/null
+++ b/reordered_power_2.c
@@ -0,0 +1,30 @@
+// 869. Reordered Power of 2
+#include "leetcode.h"
+
+/*
+ * you are given an integer 'n'. we reorder the digits in any order (including
+ * the original order) such that the leading digit is not zero. return true if
+ * an only if we can do this so that the resulting number is a power of two.
+ */
+
+static uint64_t signature(uint32_t n) {
+ uint64_t res = 0;
+ while (n) {
+ res += 1UL << ((n % 10) * 4);
+ n /= 10;
+ }
+ return res;
+}
+
+bool reorderedPowerOf2(int n) {
+ const uint64_t sig = signature(n);
+ for (int i = 0; i < 32; ++i)
+ if (sig == signature(1 << i))
+ return true;
+ return false;
+}
+
+int main() {
+ printf("%d\n", reorderedPowerOf2(1)); // expect: 1
+ printf("%d\n", reorderedPowerOf2(10)); // expect: 0
+}