Show problems as
Classic problem #869MediumHash TableMathSortingCountingEnumeration

Reordered Power of 2

Real-world version: Rearranging the Digits Into a Power of Two
Tracked in this browser only
Read Reordered Power of 2 on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Given n, its digits may be put in any order, so long as the result does not begin with a zero.

Return true when some ordering of the digits gives a power of two.

Examples

Example 1

Input
n = 46
Output
true

Its digits reorder to 64, which is two to the sixth.

Example 2

Input
n = 2
Output
true

It is already a power of two, so no reordering is needed.

Example 3

Input
n = 88
Output
false

The two-digit powers of two are 16, 32 and 64, and none of them uses two eights.

Constraints

  • 1 <= n <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def reordered_power_of2(n: int) -> bool:
Java
public boolean reorderedPowerOf2(int n)