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...