Difficulty: 🟡 Medium
Tags: Permutations, Math, Bit Manipulation
You are given an integer n
.
You may reorder the digits of n
in any way you want (including keeping them in the original order),
but the new number must not start with zero ❌0.
Return true
✅ if and only if you can rearrange the digits so that the result is a power of two ⚡.
2^k
for some integer k ≥ 0
.true
or false
.Input: n = 1
Output: true
Reason: 1 = 2^0 → already a power of two ✅
Input: n = 10
Output: false
Reason: Permutations → 10, 01(❌ invalid) → none are powers of two.
1 <= n <= 10^9
💡 Key Insight:
Instead of generating all permutations (which can be huge),
compare the digit frequency of n
with the digit frequency of all possible powers of two within the given range.
2^30
(since 2^30 = 1073741824 > 10^9
).n
.n
’s digit count matches any precomputed power-of-two digit count → return true.https://drawtocode.vercel.app/problems/reorder-power-of-2
Loading component...
Loading component...