Classic problem #869MediumHash TableMathSortingCountingEnumeration
Reordered Power of 2
Real-world version: Rearranging the Digits Into a Power of TwoTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's